开发者

PHP OOP 4/5 Compatibility?

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 ins开发者_如何学编程talled 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?


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.


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


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;


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

php4 is dead, don't use it.


< 2 years is hardly "many".

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜