473,395 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Trying to get to grips with OOP! Classes.

Hello,

Im hoping you can help me.

Im, probably not that advanced with PHP, well, I probably am, but with a
memory like a sieve, I rarely take in information...

But, Im just considering some OOP using PHP's Classes.

Seems quite handy so far, already generated a Login class and it does
tidy things up quite considerably. It means things like Error Logging
and tracking is neatly handled... pass in a Login and Password, say
"Login" and get any error messages back...

Nice.

Though too simple for my tastes. Im looking at ways to expand this,
mainly for database queries and updates... that kind of thing. I don't
know the best way, but Im reminded about work, where we use Visual Basic
and program everything on classes. To be frank, it seems a little
cumbersome and a little longwinded, but, seems nice to maintain everything.

What they have is say a Customer and a Customers class, and the
Customers class is a collection of Customer classes. It's dog slow,
seems to be unnecessarily complicated, not easily portable, but, it
seems to be quite easy to maintain.


What are peoples opinions on this? I think I've sort of got it going
with the script below but Im unsure whether Im just being stupid.

Imagine in the Actors below, that the data is coming from a table. bear
in mind also that an Actor could have a reference to a film... for example.

I'd just like to try to understand the best way to necessarily use them.

Cheers
Simon
<?php

class Actor {
var $Character;
var $ActorName;

function Character($MyReference) {
$this->Character=$MyReference;
}
function ActorN($MyReference) {
$this->ActorName=$MyReference;
}
}

class Actors {
var $ActorCount;
var $LocalActors;

function Load() {

$ItemLoad = 0;
$this->LocalActors[0] = new Actor();
$this->LocalActors[0]->Character = "Willy Wonka";
$this->LocalActors[0]->ActorName = "Johnny Depp";

$this->LocalActors[1] = new Actor();
$this->LocalActors[1]->Character = "Billy Madison";
$this->LocalActors[1]->ActorName = "Adam Sandler";

$this->LocalActors[2] = new Actor();
$this->LocalActors[2]->Character = "Herman Munster";
$this->LocalActors[2]->ActorName = "Fred Gwynne";

$this->ActorCount=3;
}

function Item($WhichItem) {
$ActorItem = new Actor();
$ActorItem = $this->LocalActors[$WhichItem];
return $ActorItem;
}
}

$DisplayActors = new Actors();
$DisplayActors->Load();

$DisplayActor = new Actor();
$DisplayActor = $DisplayActors->Item(1);
$ActorName = $DisplayActor->ActorName;

echo "Actor Name Is $ActorName\n<P>";

$LoopActors=$DisplayActors->LocalActors;

foreach ($LoopActors as $DisplayActor) {
$ActorName = $DisplayActor->ActorName;
$CharacterName = $DisplayActor->Character;
echo "\nActor $ActorName \n Character $CharacterName\n\n<P>";
}

?>
Dec 13 '05 #1
5 1421
Hi Simon,

I briefly reviewed the code you posted but i totally agree that this
model is flawed. I think (in your example Actors/Actor) there should
be a class called Film and each film will have an array (collection) of
Actor objects as a member. This would remove the "Actors" class and
just force the developer to create instances of each actor to place
into the Film.
let me know if i totally lost anyone..lol

Armando Padilla

Dec 13 '05 #2
Thing is though... what if you just want to list a series of Actors that
you have on file (this is totally fictiional - It could be Orders Order
OrderDetails OrderDetail Customers Customer etc...

eg, at work, we might have a Customer belonging to Customers as a
collection, but a Customer can belong to an Order too, and an Order
contains many OrderDetail classes which can be retrieved through
OrderDetails etc.. etc.. complicated to explain for me...

Any how, back to my original observation... yes, what if I just want to
show an "actor" listing and therefore, I just want to load a particular
selection criteria and loop through as I would with while($line =
mysql_fetch_array($result))) { } (hey, did I just remember some coding?)???

I can get the MySQL results, I can even get an array of the result and
that can be accessed by through the class... but the main program would
still need to know about the database design... Ideally, Im thinking, I
could be wrong, I'd want to load each Actor record into a set of vars
within the class to present back that the main program can then pick up
the ones it wants to display them... But that means I would need to
create lots of Actor objects... but I can't necessarily tell $this to
create an array of itself do I? Hence I think is the idea to create a
wrapper in essence, a collection of Actors that does have an Array of
Actor classes, so we can loop through those and present them back to the
main program...

does that make sense? i think I totally lost myself.

Cheers
Simon

armando padilla wrote:
Hi Simon,

I briefly reviewed the code you posted but i totally agree that this
model is flawed. I think (in your example Actors/Actor) there should
be a class called Film and each film will have an array (collection) of
Actor objects as a member. This would remove the "Actors" class and
just force the developer to create instances of each actor to place
into the Film.
let me know if i totally lost anyone..lol

Armando Padilla

Dec 13 '05 #3
Simon Dean wrote:
Thing is though... what if you just want to list a series of Actors that
you have on file (this is totally fictiional - It could be Orders Order
OrderDetails OrderDetail Customers Customer etc...
I think you may want to take a step back and think closely about what
exactly you think an object is. The <Actor> is a valid object IMO,
<Actors> is not; It is simply a collection of <Actor> Objects (a.k.a an
array in php).

If you really want to be able to produce a list of actors in an OO
manner, you may consider a class such as <ActorManager>, which when
instantiated contains a collection of actor objects. It may also have
the ability to limit the actors it 'represents' based on criteria such
as age, sex, experience, etc.

eg, at work, we might have a Customer belonging to Customers as a
collection, but a Customer can belong to an Order too, and an Order
contains many OrderDetail classes which can be retrieved through
OrderDetails etc.. etc.. complicated to explain for me...
Are you using a database to store your Customers/Actors, if so, you may
want to consider the fact that you already have a 'collection' of
Customers/Actors inherent in the db table itself. Rewriting this
collection in code without abstracting it further or adding additional
functionality gives very little reward for the effort.

The relationship between customers/orders/order details you describe is
a very standard "one to many" relationship.

Any how, back to my original observation... yes, what if I just want to
show an "actor" listing and therefore, I just want to load a particular
selection criteria and loop through as I would with while($line =
mysql_fetch_array($result))) { } (hey, did I just remember some coding?)???
Again, i would suggest something like the <ActorManager> class I
described above, for example...

/*
* print the name and age of all actors between 18 and 30 years old
*/
$manager = new ActorManager();
$manager->setMinimumAge(18);
$manager->setMaximumAge(30);

$actors = $manager->getActorList();
foreach($actors as $actor) {
echo $actor->name."\t".$actor->age."\n";
}

I can get the MySQL results, I can even get an array of the result and
that can be accessed by through the class... but the main program would
still need to know about the database design... Ideally, Im thinking, I
could be wrong, I'd want to load each Actor record into a set of vars
within the class to present back that the main program can then pick up
the ones it wants to display them... But that means I would need to
create lots of Actor objects... but I can't necessarily tell $this to
create an array of itself do I? Hence I think is the idea to create a
wrapper in essence, a collection of Actors that does have an Array of
Actor classes, so we can loop through those and present them back to the
main program...

does that make sense? i think I totally lost myself.

Cheers
Simon


Hope that helps,
Carl.
Dec 13 '05 #4
Carl wrote:
Simon Dean wrote:
Thing is though... what if you just want to list a series of Actors
that you have on file (this is totally fictiional - It could be
Orders Order OrderDetails OrderDetail Customers Customer etc...

I think you may want to take a step back and think closely about what
exactly you think an object is. The <Actor> is a valid object IMO,
<Actors> is not; It is simply a collection of <Actor> Objects (a.k.a
an array in php).

If you really want to be able to produce a list of actors in an OO
manner, you may consider a class such as <ActorManager>, which when
instantiated contains a collection of actor objects. It may also have
the ability to limit the actors it 'represents' based on criteria
such as age, sex, experience, etc.


What's the difference then, between your ActorManager and my Actors class?

Are you using a database to store your Customers/Actors, if so, you
may want to consider the fact that you already have a 'collection' of
Customers/Actors inherent in the db table itself. Rewriting this
collection in code without abstracting it further or adding
additional functionality gives very little reward for the effort.
Don't get me wrong, I quite agree. This is just the structure we use at
work where we get everything from the database and represent everything
in classes and then more... It's never made much sense to me, because
nothing is really being simplified in the process... the error handling,
save and load routines are being standardised though I guess... but
that's about it....

The relationship between customers/orders/order details you describe
is a very standard "one to many" relationship.

Any how, back to my original observation... yes, what if I just
want to show an "actor" listing and therefore, I just want to load
a particular selection criteria and loop through as I would with
while($line = mysql_fetch_array($result))) { } (hey, did I just
remember some coding?)???

Again, i would suggest something like the <ActorManager> class I
described above, for example...

/* * print the name and age of all actors between 18 and 30 years old
*/ $manager = new ActorManager(); $manager->setMinimumAge(18);
$manager->setMaximumAge(30);

$actors = $manager->getActorList(); foreach($actors as $actor) { echo
$actor->name."\t".$actor->age."\n"; }


In this instance, presumably the set's are a backend to produce a where
statement, the getActorList loads the query data into a selection of
Actor objects, which again, doesn't sound too different to my Actors
class... perhaps I never explained it properly...
Thanks
Simon
Dec 14 '05 #5
Simon Dean wrote:
Carl wrote:
Simon Dean wrote:
Thing is though... what if you just want to list a series of Actors
that you have on file (this is totally fictiional - It could be
Orders Order OrderDetails OrderDetail Customers Customer etc...
I think you may want to take a step back and think closely about what
exactly you think an object is. The <Actor> is a valid object IMO,
<Actors> is not; It is simply a collection of <Actor> Objects (a.k.a
an array in php).

If you really want to be able to produce a list of actors in an OO
manner, you may consider a class such as <ActorManager>, which when
instantiated contains a collection of actor objects. It may also have
the ability to limit the actors it 'represents' based on criteria
such as age, sex, experience, etc.

What's the difference then, between your ActorManager and my Actors class?

It is not really much different; The fundamental difference being the
disambiguation of the actual role of the object that produces the list
of actors. Having an <Actors> class is no better to me than simply
loading an array with actors, and does not satisfy _my_ requirements for
a productive object; the creation of a list of actors with no additional
functionality would just as well be implemented in a method rather than
a class.

An <ActorManager> class should provide methods to handle those
responsibilities that you would expect of a Manager/Agent responsible
for managing actors. i.e. select all actors that are available for work,
all actors of a certain age, only female actors, etc... An ambiguous
<Actors> class suggest no other responsibility other than being a
collection of actors, something that an array in php will already handle
quite well.
Are you using a database to store your Customers/Actors, if so, you
may want to consider the fact that you already have a 'collection' of
Customers/Actors inherent in the db table itself. Rewriting this
collection in code without abstracting it further or adding additional
functionality gives very little reward for the effort.

Don't get me wrong, I quite agree. This is just the structure we use at
work where we get everything from the database and represent everything
in classes and then more... It's never made much sense to me, because
nothing is really being simplified in the process... the error handling,
save and load routines are being standardised though I guess... but
that's about it....


I understand, I've seen this method used before and am not suggesting it
is 'wrong'. Still, I firmly believe that by using it, you are not taking
full advantage of object oriented programming.

The relationship between customers/orders/order details you describe
is a very standard "one to many" relationship.

Any how, back to my original observation... yes, what if I just want
to show an "actor" listing and therefore, I just want to load a
particular selection criteria and loop through as I would with
while($line = mysql_fetch_array($result))) { } (hey, did I just
remember some coding?)???


Again, i would suggest something like the <ActorManager> class I
described above, for example...

/* * print the name and age of all actors between 18 and 30 years old
*/ $manager = new ActorManager(); $manager->setMinimumAge(18);
$manager->setMaximumAge(30);

$actors = $manager->getActorList(); foreach($actors as $actor) { echo
$actor->name."\t".$actor->age."\n"; }

In this instance, presumably the set's are a backend to produce a where
statement, the getActorList loads the query data into a selection of
Actor objects, which again, doesn't sound too different to my Actors
class... perhaps I never explained it properly...


There is a good possibility that the implementation of your method is
not very different from mine. Perhaps the difference of opinion is a
result of semantics. When thinking about object oriented design, i
particularly like this definition of object (from
http://wordnet.princeton.edu/perl/webwn?s=object ) :
"(a tangible and visible entity; an entity that can cast a shadow) "it
was full of rackets, balls and other objects""

Although it is not always possible _or even ideal_ to make this a firm
rule, I find if beneficial to define objects in a manner that make its
purpose, actions, attributes and features as clear as possible.

Cheers,
Carl.
Dec 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Savut | last post by:
I know there is a lot o people asking me if PHP5 is fully OOP, I said no, but nobody trust me and I dont care as I have no time to explain you all. But I find this from ORACLE and so I decided to...
75
by: projecktzero | last post by:
I know this might not be the correct group to post this, but I thought I'd start here. A co-worker considers himself "old school" in that he hasn't seen the light of OOP.(It might be because...
77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
51
by: nospam | last post by:
THIS IS the DOTNETJUNKIES MESSAGE ------------------------- We're Sorry As many of you know we have recently launched SqlJunkies.com. We have overhauled our runtime and will be using it on...
4
by: Deniz Bahar | last post by:
Hello, A couple days ago my friend (OOP guy) shows me what OOP was all about in C++. This morning I figured I can do pretty much the same thing with C (by putting function pointers in...
11
by: john andrew | last post by:
-- hello When using properties with OOP, VB6 had Get/Let and VB.net has Readonly/and .... Are theses OOP concepts with Property use or only specific to Visual Basic properties, With this...
0
by: Luke Herbert | last post by:
I am very new to C# and have been trying to get to grips with it by writing a small tool to detect a USB key being inserted and then backup user specified files to the key. I have written some...
18
by: dancer | last post by:
I haven't given up yet, but I'm wondering if OOP is worth the effort. I know that almost everybody in this forum thinks OOP is the only way to go, but is there anybody out there who believes...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.