473,883 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($MyRe ference) {
$this->Character=$MyR eference;
}
function ActorN($MyRefer ence) {
$this->ActorName=$MyR eference;
}
}

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=$Di splayActors->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 1450
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_arr ay($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_arr ay($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".$act or->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_arr ay($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".$act or->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
responsibilitie s 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_arr ay($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".$act or->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
2119
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 post it to prove what I said. ASP.NET is OOP not PHP (not yet). http://otn.oracle.com/pub/articles/hull_asp.html I program 80% of my projects in php, so dont tell me I'm a MS ASP lover, but what I want to said is we have to admit PHP is not...
75
3523
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 he's in love with Perl...but that's another story.) He thinks that OOP has more overhead and is slower than programs written the procedural way. I poked around google, but I don't know the magic words to put in to prove or disprove his assertion. Can...
77
5701
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 database from SQL Server to Oracle or DB2 or vice versa... and that's it.... And a lot of these enterprises don't need it as they already know what database they are going to use and they don't plan on switching in and out database in the first...
51
4086
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 DotNetJunkies.com also. -------------------------------------------------------------------------- YEP, DOTNET JUNKIES REDESIGN....
4
2248
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 structures and using Macros). I've gone pretty far but I'm running into problems because the function pointers within the structures need an extra argument (a pointer to that type of structure) so they know which data to operate on. Also, I have to...
11
1799
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 property use does C++ have this as well or just Visual C++.?
0
4096
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 code and been able to construct a WMI query and can even get the output of this query to display to the console but not to a TextBox on my form. I fear it is embarassingly obvious but having looked at a whole heap of books (not read them in their...
18
1435
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 differently? 1. Is there anybody who believes that programming patterns will change in the near future and leave OOP behind? 2. I *love* Asp.net. Is there any language to use presently in ASP.net that uses the TRADITIONAL method of programming? ...
0
9792
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10745
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10848
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10417
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9575
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7130
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5798
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5992
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.