Forum Moderators: coopster
How about this. Why not look at some open source PHP projects use classes? There are zillions of them. Dig around and see how they use classes.
phpclasses.org has a couple of forum packages available
[en.static.phpclasses.org...]
Look at what they have and then report back to us!
Tom
- it seems to me that the fundamental model for PHP is procedural. This is partly because the script is reloaded with every page and you don't usually have multiple instances running. When I look at most PHP that uses classes, I usually don't see much gain. The chief advantage I see in PHP is just in the case of complex constructors that allow you to simply instantiate a new instance.
- PHP classes don't allow the differentiation of public/private/protected members, so the protection that should be enforced by OOP is just sort of suggested by PHP. This will be added in PHP5.
- PHP passes classes by value, as opposed to most OOP languages which pass classes by reference. In other words, a copy of the entire class gets made, potentially leading to significant overhead. This also wil be fixed in PHP5.
Now, please, some PHP OOP guru jump in and correct this! Give a few examples of places where you think PHP classes really do things, or do things much better, than would a function library.
Tom
eg. using classes, you might be able to write something like:
if ($newmsg) {
$newpostid = myForumClass->insert($forumid, $newmsg);
}
I know that's a pretty pathetic example, but it should illustrate the idea. The benefits are: (a) your code becomes a lot easier to read; and (b) you can change the inner workings of your class/functions without having to re-visit your forum code.
OOP is more of an art then a science (at least when you start). It takes a bit of practice to be able to jump straight to a useful 'abstraction' model...
Tom's (ergophobe) points are bang on. Having looked at the roadmap for PHP5, I also don't hold much hope for the language (for OO work)... it's still a great prodedural scripting language though, and I use it regularly for that.
bobnew32- If you want to learn OOP, learn OO theory. It was hard for me to go from a procedural mind-set to an OO one, and few books cover that. I can wholedheartedly recommend "Design Patterns Explained: A New Perspective on Object-Oriented Design" (ISBN: 0201715945) as the best introduction I have ever read to OO because it really made that transition clear to me.
You will probably feel limited by PHP5, but doing it this way you have transferable skills to other languages, including Java, C++ and Python.
Okay, the more I look into it, the more I warm to PHP OOP... That said, I think it's still a bit half-baked.
Weakness in PHP object implementation
Let's start with a few quotes from Zeev Suraski, co-creator of PHP, regarding the OO Evolution of PHP [devx.com]:
When classes were introduced to the code base of what was to become PHP 3.0, they were added as syntactic sugar for accessing collections. PHP already had the notion of associative array collections, and the new classes were nothing but a neat new way of accessing them.[PHP 4 and the Zend engine], however, left PHP's object model mostly unchanged from version 3—it was still very simple. Objects were still very much syntactic sugar for associative arrays, and didn't offer users too many additional features.
So, what could one do with objects back in the days of PHP 3.0 or, for that matter, with the current PHP 4.0 version? Not much, really.
Moving right along... Regarding drcrombie's comments, I would sort of disagree. I would not say that the principle advantage of OOP is that it "removes the detail from your programming." I think this can mostly be achieved with good coding, be it OOP, functional or procedural. Given that classes in PHP are essentially associative arrays with methods, there isn't really much difference between $john->id and $john['id'] or $john->getID() and getID($john) except that in each case the former is going to be slower and take a lot more memory (see Seev Suraski's comments below).
Advantages of OOP that often get mentioned
1. Code reuse
2. Maintainibility
Again, can't both be achieved pretty well with library functions? If you are disciplined and use proper abstraction, there shouldn't be that much difference. Abstraction, a feature of most OO languages, is key, but it is poorly executed in the PHP object model (see below) and can in any case be achieved in pretty much any language. The idea and the practice way pre-date OOP.
The advantages of OOP should be (as they are in most languages):
1. Inheritance - okay PHP has that (or single inheritance at least, which is mostly good enough right?)
2. Abstraction or Encapsulation - basically the class provides an efficient way to access data and provides the only way to access private data. Though possible (read: recommended (read: required)) in procedural programming, OOP tends to encourage it more (see links at the end of this post on abstraction)
Poor abstraction in PHP.
This is the real weak point in PHP as it stands now. Again, Zeev Suraski:
The most problematic aspect of the PHP 3 / PHP 4 object model was that objects were passed around by value, and not by reference.Objects in PHP 3.0 and 4.0 are not 'special'. They behave like any other kind of variable
While PHP 3 and 4 did address these problems to a certain extent by providing syntactic hacks to pass around objects by reference, they never addressed the core of the problem, which is that objects and other types of values are not created equal. Therefore, objects should be passed around by reference unless stated otherwise.
Proposed fix in PHP5
The plan is to fix this for Zend 2/ PHP5. the object model will be redone so that objects are passed by reference (sort of) and will have private, public and protected methods and objects. According to Suraski, this will address the problems noted above:
First, it means that your applications will run faster, because passing objects by reference requires much less data copying... Obviously, another direct result of this is that it saves a significant amount of memory... No longer will you have to worry about whether changes you make to the object inside the constructor will survive the dreaded new-operator behavior.
At that point I think it becomes a lot more efficient and more interesting and I might dive in. Maybe I'll dive in now...
Promised links on abstraction
I've mentioned this before. Many people think that Abelson and Sussman's Structure and Interpretation of Computer Programs [mitpress.mit.edu] is the finest programming book ever written and it is now fully available online [mitpress.mit.edu]. You find the following chapters especially worth checking out:
2.1 Introduction to Data Abstraction [mitpress.mit.edu]
1.1.8. Procedures as Black-Box Abstractions [mitpress.mit.edu]
1.3 Formulating Abstractions with Higher-Order Procedures [mitpress.mit.edu]
Tom