0

Does this kind of inheritance work for php4?

class A {
    var $string;

    function A () {
        $this->string = "Hello";
    }
}

class B extends A {

    function B () {
        $this->string = "World";
    }
}

class C extends B {

    function C () {
        $this->string = "FooBar";
    }
}

$a = new A();
$b = new B();
$c = new C();
                    // OUTPUT:
echo $a->string;    //   "Hello"
echo $b->string;    //   "World"
echo $c->string;    //   "FooBar"
3
  • 4
    can i ask, why are you using php4 ? Commented Mar 14, 2011 at 10:31
  • Sure, because i am working on an open source project that allows the user to download the whole software package and install it on their system. We wanted to make this as easy as possible for the user and wanted to make it compatible for PHP4/PHP5 & MySQL4/MySQL5. So that almost all machine can install the package with a few simple clicks without having to change much on their current system. Commented Mar 14, 2011 at 12:29
  • 1
    You should really not build anything on top of an outdated and unsupported platform. Commented Mar 14, 2011 at 13:46

2 Answers 2

1

Firstly when your doing cross compatibility you should create a cross bread application that runs to the best standards on each platform.

The way I would do this is sections of the the application that require PHP 5 specifics you would create a compatible directory and load the compatible file for PHP4

for example:

/application/classes/database/core.php
/application/classes/database/util.php
/application/classes/database/misc.php
/compat/application/classes/database/core.php
/compat/application/classes/database/util.php
/compat/application/classes/database/misc.php

is you can then do:

function loadClass($path)
{
    if (version_compare(PHP_VERSION, '5.0.0', '<'))
    {
         $path = "/compat/" . $path;
    }

    require_once $path;
}

loadClass("/application/classes/database/core.php");

then as time goes on and nobody is using PHP 4 you can simply drop the compat directory and remove the check from loadClass.

an alternative would be to register an autoloader, this way you can program using require_once within your application and reduce the need for an extra function to be palced through out your application.

and answer your questions specifically about hte class stated above, this is perfectly cross compatible.

If you stick with creating your application for PHP4 specifically you should take into consideration:

  • Do not use public / private modifiers for your class methods.
  • Always use the class name as the constructor instead of __construct.
  • When passing a class to a function, you should always pass by reference using the & symbol.
  • Do not use magic methods such as __tostring or __destruct.

You should always create you application on a PHP4 system, and then test on the latest version of PHP5. (amend accordingly)

Sign up to request clarification or add additional context in comments.

1 Comment

I will take your advice to heart. Some things i have already implemented or taken into account, others i didn't really think about yet. Great answer!
1

Looking at the notes from PHP Reference pages, there isn't any mentioning whether multi-level inheritance wasn't supported in php4. Therefore, it should work with version 4.

Perhaps, someone having a PHP 4 interpreter can assure.

2 Comments

the main thing that separates PHP 4 vs PHP 5 is that fact that in PHP 5 classes are always passed by reference, and also the way there constructors work, other than that you right, there pretty much the same, bar a few magic methods.
Well i am asking this question because there are a lot of rumors about php and inheritance on the internet that claim that it doesn't work all too well on php4. I'm just not sure if it included multi-level inheritance as well...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.