Forum Moderators: coopster

Message Too Old, No Replies

classes question

         

bobnew32

5:52 pm on Nov 30, 2003 (gmt 0)

10+ Year Member



I'm 80% sure what classes are and how to use them code wise, but how would I use classes in making a forum? I have a forum and website dealing with television and movie reviews in the making, and I want it up to par as much as it can be and making it class compliant would be perfect; but i'm unsure how to use them correctly in these uses, care to shed a little light? Aint oop fun? :)

bobnew32

10:16 pm on Dec 6, 2003 (gmt 0)

10+ Year Member



Anyone?

ergophobe

11:10 pm on Dec 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've been hoping folks would jump in with words of wisdom, but I guess not.

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

bobnew32

12:44 am on Dec 7, 2003 (gmt 0)

10+ Year Member



I've been trying to learn classes... I only asked for some php theory thats all.

ergophobe

1:36 am on Dec 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, I'd love to help but I really can't since I just don't know much about it. I have to admit that I haven't really seen the advantage of classes over function libraries in PHP. That's partly because of the way PHP implements things, but mostly just my ignorance. Since nobody wants to speak from wisdom and knowledge, I'll throw out some possibly ill-informed statement and maybe at least get some argument going!

- 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

dcrombie

2:48 pm on Dec 7, 2003 (gmt 0)



I'm a bit rusty on the jargon - but my concept of classes and OOP is that it removes the detail from your programming.

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

danieljean

4:35 pm on Dec 7, 2003 (gmt 0)

10+ Year Member



IMO, any OOP guru would find PHP's class support a waste of time. I'm certainly no guru, but after applying design patterns to Java I cringe every time I work with PHP.

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.

ergophobe

7:20 pm on Dec 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Danieljean posted while I was writing this tome (sorry), but here it is.

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