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!