php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #42256 PHP4 backwards-compatibility slightly broken
Submitted: 2007-08-09 13:22 UTC Modified: 2007-08-16 09:14 UTC
From: mat at bcclimited dot co dot uk Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.2.4RC1 OS: Windows, Linux
Private report: No CVE-ID: None
 [2007-08-09 13:22 UTC] mat at bcclimited dot co dot uk
Description:
------------
PHP4-style constructors do not function, in PHP5, in the same way as they do in PHP4.
The issue lies in when parent constructors are called, or not as the case may be.

Reproduce code:
---------------
class users_base {
    function Users() {
        echo "users_base::Users()\n";
    }
}
class Users {
    // Nothing here.
}

$base = new users_base();
$child = new Users();

Expected result:
----------------
Running the sample code in PHP4 will give the expected result:
users_base::Users()
users_base::Users()

Whereas PHP5 will not call the Users() function in the parent class:
users_base::Users()

I class this as a bug because of the expected PHP5 behaviour as well as the evident PHP4 behaviour. If the users_base function is renamed to __construct(), it will get called on BOTH instantiations, as PHP4 does with its style.

Actual result:
--------------
users_base::Users()

(The child class does not call Users() on instantiation)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-09 13:26 UTC] mat at bcclimited dot co dot uk
Oh, thought I'd add a workaround:

class Users {
    function Users() {
        parent::Users();
    }
}

Unfortunately for me, there are one hell of a lot of classes that our previous programmer built in this fashion, making the workaround impractical.
 [2007-08-09 13:40 UTC] mat at bcclimited dot co dot uk
Nuts, typo. Both the `Users` should start:
class Users extends users_base {
 [2007-08-09 15:01 UTC] jani@php.net
Reclassified.
 [2007-08-10 01:25 UTC] judas dot iscariote at gmail dot com
Interesting ;) the documentation should also mention that the old PHP4 beahviuor is clearly the wrong thing.
 [2007-08-10 09:41 UTC] mat at bcclimited dot co dot uk
I wouldn't say "clearly"... It kind of makes sense the way PHP4 works, and I'd expect that any features that claim to be backwards-compatible work in the same way as the old version, else the claim is fallacious.

I assume that the PHP4 interpreter looks at all available functions for one matching the class name and calls it if it exists, whereas PHP5 only looks in the functions implemented in that class and does not traverse up to the parent - making it behave as if the function was not inherited.

I take it this is a "will not fix"/"not broken" status, and I need to recode all the classes that are constructed like this? :(
 [2007-08-16 08:51 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"In PHP 5, function with the name of a class is called as a constructor only if defined in the same class. In PHP 4, it is called also if defined in the parent class."
 [2007-08-16 09:14 UTC] mat at bcclimited dot co dot uk
I still disagree that this is merely a documentation issue, how can 5 claim to be backwards-compatible when classes constructed like this don't work anymore?

"For ***backwards compatibility***, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics."
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Aug 06 07:00:03 2025 UTC