PHP 5's OOP vs PHP 4's

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
figaro11
Forum Commoner
Posts: 64
Joined: Mon Sep 17, 2007 11:49 pm

PHP 5's OOP vs PHP 4's

Post by figaro11 »

I went to php.net's documentation, to learn about classes and such, only to find that there two different documentations of Classes and Objects each for PHP 5 and 4.

Now I don't know which one to read. I know I have php 4 on my server with a php 5 interpreter. I could activate my php 5 interpreter on my site, but I don't want to mess up any current php scripts.

So what I'd like to know is what are the major differences in the Objects and Classes in php 5 and 4? Is there really a big change?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I would recommend trying PHP5 as there are usually few or no problems upgrading. PHP4 will no longer be supported.

I just upgraded a server that hosted many sites using PHP4. The process was pretty easy. In fact, PHP5 actually found a few bugs!. The main thing I found was that old SERVER vars had to be renamed. One of our members, Infolock, just wrote an article for php|architect about this I believe.
(#10850)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

PHP4 isn't exactly "real" OOP, as it is missing a lot and is generally considered hackish. PHP5 is much more pure.
User avatar
figaro11
Forum Commoner
Posts: 64
Joined: Mon Sep 17, 2007 11:49 pm

Post by figaro11 »

superdezign wrote:PHP4 isn't exactly "real" OOP, as it is missing a lot and is generally considered hackish. PHP5 is much more pure.
Okay. PHP 5 looks great. But what I'm concerned about is if I changed my php compiler over to php 5, what should I expect, error wise? What major differences are there between the two that might effect my current php scripts?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

I'm still really coming to grips with php5 oop, so I cant say I'm a really good judge of exactly what has changed.

I have however just switched a few classes made in php4 to a php5 environment, and had surprisingly few issues.

Actually, to be honest, I can only recall two problems (though each one numerous times).

Class variable definition changes from

Code: Select all

var $myClassVar;
to

Code: Select all

 public $myClassVar;
And creating instances by reference is depreciated. So

Code: Select all

$instance = &new myClass();
should be

Code: Select all

$instance = new myClass();
in php5, however the behavior is the same.

There were some minor issues elsewhere, but nothing specifically OOP related. I'm certainly not saying that other issues might not be present, I'm just saying these were the only real issues in my conversions. But then, my php4 classes were pretty basic.

Hope this helps
Post Reply