Forum Moderators: coopster

Message Too Old, No Replies

PHP 4 vs PHP 5

which version is better for developing brand new site

         

lasem

4:05 am on Sep 20, 2007 (gmt 0)

10+ Year Member



I'm launching a brand new site and will be using php as the language. I'm not a web developer so I'm throwing the question out to PHP experts. Should the site be coded in PHP4 or PHP5? Does it really matter from a technology standpoint to have the latest technology? Does it really matter from a business standpoint to have the latest technology?

As a brand new site, I‘m starting from a clean slate – so why not use the latest stable version?

Habtom

5:18 am on Sep 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I recommend PHP5.

It will save you from unnecessary migrations to the newer PHP versions.

vincevincevince

5:41 am on Sep 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP4 is already slated for end-of-life. Build for PHP5, but if you want maximum flexibility keep your options open by not using functions which aren't also available in PHP4. If you do that then you'll be able to put your site on a PHP4 or a PHP5 server without having problems on either.

Personally I now do everything for PHP 5 and make heavy use of the object oriented PHP functions (http://www.php.net/oop5).

jatar_k

12:13 pm on Sep 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld lasem,

if you plan on doing anything with objects then you must use php5
as mentioned above, you'll have to switch eventually so might as well start out that way

there really aren't huge differences in coding for one or the other, php 5 really does afford you the most options though

whoisgregg

4:46 pm on Sep 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only argument one can make for developing new projects in PHP 4 is web host support. Not all web hosting companies support PHP 5 yet.

However, I have a theory about web hosts that don't offer PHP 5 support yet. If they can't figure out how to provide PHP 5 support, they are probably going to have trouble with other technical support issues. Pick a host that can keep up with technology.

pinterface

6:12 pm on Sep 20, 2007 (gmt 0)

10+ Year Member



From a business standpoint, it doesn't matter.

From a technological standpoint, I've been pretty frustrated with PHP5; but as has been hinted at already, if you don't want to go through the gratuitous and sometimes ugly changes necessary to get valid PHP4 code working in PHP5, just start with 5. You're eventually going to be stuck with it anyway.


Here are some differences I've run into during a recent PHP4 to PHP5 conversion:

Constructors (+1 for PHP5)
PHP4: Constructors are named after their class. If you change the name of a class, you have to find all callers of the constructors. Ew!
PHP5: Constructors are named __construct. If no __construct method is found, falls back to PHP4 behavior.

Destructors (+1 for PHP5)
PHP4: No such concept.
PHP5: Destructors are named __destruct.

$this = new Class(...); (+2 for PHP4)
PHP4: $this can be reassigned, allowing one to change the class implementing an object. A very useful feature and one I used quite a lot.
PHP5: Assignment to $this is explicitly forbidden. Factory methods are the recommended substitute, but they're ugly.

array_shift($args = func_get_args()); (+1 for PHP4)
PHP4: If func_get_args() returns [a, b, c], $args is [b, c]--as expected.
PHP5: If func_get_args() returns [a, b, c], $args is [a, b, c]. Good luck hunting down that bug.

Copying through Assignment (+1 for PHP4)
$o1 = new Object(...); $o2 = $o1;
$o1->setThing('x'); $o2->setThing('y');
PHP4: $o1 and $o2 are different objects, with Thing set to 'x' and 'y', respectively. This behavior matches that of strings, numbers, and other non-object types, which are also copied on assignment.
PHP5: $o1 and $o2 are the same object, with Thing set to 'y'. This behavior contradicts that of strings, numbers, and other non-object types which do produce copies. A clone operator was introduced to work around this (provided your objects also implement __clone methods); however, the clone operator only works on objects, not strings or numbers, so ensuring you've produced a copy is now along the lines of

$o2 = is_object($o1) ? clone $o1 : $o1;
. Ick.


And some differences which might get used now that I've transitioned to PHP5:

Public, Protected, Private Class Variables (no point)
PHP4: All class variables are public.
PHP5: Class variables can have differing access levels. This annoys me, but some people like it.

Class Constants (+0.5 for PHP5)
PHP4: No such thing. I work around this, and the lack of namespacing in general, by using Class__CONSTANT.
PHP5: Has them as Class::CONSTANT. It isn't namespace support, and has quite a few caveats, but it's sort of an improvement if you squint just right.

Autoloading (+1 for PHP5)
PHP4: No such thing. If you want to load classes, you'd better make sure there are require_once statements all over the place. Or write an object-creation function (make_instance) which automatically loads any class that doesn't exist.

make_instance('Class', ...)
isn't nearly as pretty as
new Class(...)
, though.
PHP5: Autoloading exists, provided you write an __autoload function. No more does require_once need to be littered around one's code. A marginal savings overall, but still an improvement.


However! You're starting with a blank slate, which certainly changes the perspective. PHP5 itself is fine, it's just transitioning to it from 4 which sucks. :)


To summarize, with apologies to Robert Frost:

Some say you should write in five,
some say in four.
From what I know transitioning
I hold with those who favour five.
But if I had to write it twice,
I think I know enough of things
To know that for websites four
Is also great
And would suffice.