tags:

views:

64

answers:

6

I've read that major changes were made in PHP 5 to the OO structure, so I'm concerned (before taking up OOP PHP) that users would need a specific version of PHP installed to run my application (either 4 or 5). Up until now this hasn't really been an issue as it's just been differences like missing functions or different return values.

So would an OOP structured PHP application designed for PHP 4 run fine under PHP 5, or vice versa?

+8  A: 

For the most part, things written using PHP 4 OOP should work in PHP 5.

However, support for PHP 4 has been dropped many years ago. It doesn't even receive security fixes anymore. There is no reason whatsoever to run PHP 4 today.

Daniel Egeberg
The reason I ask is that unfortunately a lot of web hosts still limit their users to PHP 4. Do you think a PHP 5 focused OO application would work under PHP 4?
Alex
@Alex: if a hoster forces you to use php4, stay away from that hoster.
smoove666
A: 

I would not worry about that, as php4 is no longer supported (read: no security fixes).

php4 is dead, don't use it.

smoove666
+1  A: 

Yes, it is possible to make your code run on both 4 and 5, for example famouse php frameworks such as CodeIgniter and CakePHP also support php4 apart from php5.

Also keep the fact in mind that php4 isn't used as much as php5. You need to have a look at:

Differences between PHP4 and PHP5

Sarfraz
+1  A: 

One of the biggest changes in magic methods that arrived in PHP5. For example, you can do some initializing when an instance of your object is created using the __construct() magic method. However, in PHP4 environment, this method wouldn't be executed.

A way around this is, to create a method with the same name as your class. For example:

<?php
class SomeName {

    function __construct() {
        // do some stuff here
    }

    function SomeName() {
        $this->__construct();
        // for PHP 4 support; executes __construct on class initialization
    }
}
$class = new SomeName;
Martin Bean
A: 

< 2 years is hardly "many".

http://news.cnet.com/2100-1046_3-6196973.html

Jason
A: 

You can do much nicer things with PHP5's OO than PHP4's. I just took the decision to break PHP4 compatibility and it has significantly improved my code.

Having proper public/private methods is nice, and the new __toString() method is interesting too.

I don't think many hosts now would still limit you to PHP4. That might have been the case a year ago, but certainly not now. I did a straw poll of users and less than 10% were on PHP4. Of those, most would be willing to upgrade.

Jhong