One of the best introductions to classes I have ever seen...

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

What helped me grasp it was a real-world example though


let's say in your shopping cart system you have a cart class, the cart can have a registry to hold other objects. Your class has methods for interacting with the objects it contains. You can then have multiple cart objects, serialize() the cart object for storing for later use, etc...
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Grim... wrote:It still doesn't explain what I find most confusing about classes - why bother with them?
Taking his Page class as an example, why not just use functions:

Code: Select all

function Display($Title, $Keywords, $Content) {
	echo "<HTML>\n<HEAD>\n";
	DisplayTitle($Title);
	DisplayKeywords($Keywords);
	echo "\n</HEAD>\n<BODY>\n";
	echo $Content;
	echo "\n</BODY>\n</HTML>\n";
}

function DisplayTitle( ) {
	echo "<TITLE>" . $Title . "</TITLE>\n";
}

function DisplayKeywords( ) {
	echo '<META NAME="keywords" CONTENT="' . $Keywords . '">';
}
Less code, and easier to read, IMHO.
That is the worst example of OOP I have ever seen :lol:

The primary purpose of OOP is reusability (encapsulation, etc...)

By hard coding any interface to a class you loose it's reusability...under the cojntext of PHP anyways...slightly different in Windows application development...

Anyways, yea, bad example...

OOP has it's place, as does procedural programming...neither should be used for the above example though...

A good example of OOP IMHO is the phpMailer class...takes care of all the BS for you in a nice easy to use class...as opposed to writing all the proper headers, etc...

But I've always disliked those OOP intro articles where they use a jet plane or car...with member functions like:

Code: Select all

startEngines()
Just my two cents...

Cheers :)
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

the book

PHP 5 Objects, Patterns, and Practice
by Matt Zandstra

is a good read and has a very good introduction to class' and all the php5 functions.

its free so you can download it online or if your like me and prefer to have it in paper, you can purchase it too.

EDIT: ah, it actually isnt...
Last edited by mickd on Tue Jan 03, 2006 4:10 pm, edited 1 time in total.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Free download? Cool. :)

I'll check it out.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

i don't see what's unreusable about his code..there's only so many ways to create a basic HTML page...
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

Hmmm.. I am gonna take that as a typo - you dont see what is *UN* re-usable about the example code?

In the event you meant to say "I dont see whats so re-usable about it" , as in "why should it be a class, and not procedural?", my answer to taht would be :

that code is a crappy example to show code re-use :)

OOP is about being able to create a "generic" container which can do many things - IIRC, there is a similar thread in these forums discussing *exactly* that topic-

Basically with procedural , lets say you have 5 pages with *similar* but not identical database queries on the same set of tables.. Now you can write procedural 5 times , and tweak each query to whatever you need, and you're fine and dandy . However, each and every time you make a change to teh database structure, you have to go back over *all 5* of those pages and re-do them and debug and sort through different issues ..

With Classes, you can put it all into a single class container, and write main functions and sub functions that could potentially use only one or two queries based on type of data required, and then simply do something like

Code: Select all

$myclass = new Class;
$my_required_datatype = "last_login";
$myclass->type = $my_required_datatype;//initialize teh class type value 
$myclass->depth = 6; //set it to return 6 rows
$myclass->query($page,$value,$date);
//query user table for the logins of specific users on a given data or date range?
//next up
$myclass->depth = 25;
$myclass->type = "referrals";
$myclass->query($page,$value,$date);
//poerhaps query users table for who has the most referrals, etc
Now, imagine you have 5 pages of similar stuff, and then a database design change (say added a new user feature)

If you had procedural code, and the new feature was such that every page needed to have the sql query modified- you would have to remember which 5 pages (out of how many? 15 - 20?) had the query that you need to change, and then dig them out and change each query , debug and test.

With a class, you only need to go to teh class.php file, and fix the queries there, and run your (already prepared during previous debugging) unit testing library on the class...

With a large project (or enterprise class) , doing OOP in this way can save you hundreds of hours of frustration and headaches , because all your code is in one place, and you would have your unit tests already prepared to make sure that your changes didnt break anything..

and if you need a new function or query type for teh new project , you have two options - you can add it in to the class itself, or you can merely *extend* the class to add your own custom function..

OOP has its place, however it doesnt need to be used *exclusively* ..

For example, if I was going to write up a unique page for teh system, and I, in my analysis, determined that this page was the only place a particular process would be used, I would write the whole page in Procedural, because building a class for it would be over-engineering it.

With HTML pages- SOME things can be done in classes- look at smarty, for instance- you should try out smarty templates and see how much time it can save you - once I learned to use it, I would not *DREAM* of doing *ANY* web project without it.. all my projects (for work, or personal) use Smarty template system , it's that efficient :)

Generally classes are good for code re-use and maintainability where you may use teh same method or group of methods in many different places , and if you find a bug in one place, using classes would mean you only need to fix it *ONCE* , with procedural, you have to go back through your code and fix it *everywhere* you have ever used that same method :) now if you had a codebase comprising 123 different php files (yes one hundred twenty-three files.... and counting... if it sounds like Im talking about code I write for work, It's cause I am.. ) , would you rather *know* your method was in just *one* place, or would you rather try to remember which of those 123 files contains this bug you just found ??

My rule of thumb is: If the code is unique to a page or two, and really isnt gonna be used for much more than *this* project, do it in procedural , but if I think I may use this same code across multiple projects, and potentially even with different database tables or structures, etc, I will more likely write the code as a class..

Just my 2 cents :)

Basically, I look at it as personal preference, and an analysis of exactly what code you're doing... which determines how you'll code it. :)

OOP can be a real time saver.. OOP can also be a pain in the ass

Bri!
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

where exactly can i find that book as a free download? i appear to be having some difficulties.
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

oops my mistake, remember reading on this forum that, that book was free to download, guess it wasnt :? sorry guys.
Post Reply