Trying to get to grips with OOP! Classes.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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...


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.



class Actor {
    var $Character;
    var $ActorName;
    function Character($MyReference) {
    function ActorN($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";

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

$DisplayActors = new Actors();

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

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


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


Re: Trying to get to grips with OOP! Classes.

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

Armando Padilla

Re: Trying to get to grips with OOP! Classes.

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.


armando padilla wrote:
Quoted text here. Click to load it

Re: Trying to get to grips with OOP! Classes.

Simon Dean wrote:
Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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();

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

Quoted text here. Click to load it

Hope that helps,

Re: Trying to get to grips with OOP! Classes.

Carl wrote:
Quoted text here. Click to load it

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

Quoted text here. Click to load it

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....

Quoted text here. Click to load it

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...


Re: Trying to get to grips with OOP! Classes.

Simon Dean wrote:
Quoted text here. Click to load it

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.

Quoted text here. Click to load 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.

Quoted text here. Click to load it

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 ) :
"(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.


Site Timeline