Take the State of PHP 2026 Survey →

Remove PHP 4 Constructors View as Markdown

Implemented (PHP 7.0)
Authors
Levi Morrison levim@php.net
Date
2014-11-17
Version
1.0
Source
wiki.php.net/rfc/remove_php4_constructors

Vote

remove_php4_constructors closed
Yes
50
No
4
54 voters
ab No 2015-02-27 15:32
aharvey Yes 2015-02-26 19:27
ajf Yes 2015-02-23 15:30
brandon Yes 2015-03-03 13:55
bwoebi Yes 2015-02-23 13:47
crodas Yes 2015-02-23 19:29
daverandom Yes 2015-03-06 15:04
davey Yes 2015-02-25 07:40
diegopires Yes 2015-03-06 12:47
dm Yes 2015-02-23 16:12
dmitry Yes 2015-02-26 23:39
dragoonis Yes 2015-02-23 22:17
frozenfire Yes 2015-02-23 04:49
galvao Yes 2015-02-23 04:55
guilhermeblanco Yes 2015-02-23 04:45
indeyets Yes 2015-02-23 10:40
jedibc Yes 2015-02-23 08:35
jgmdev Yes 2015-02-25 05:45
jpauli Yes 2015-03-02 17:48
jwage Yes 2015-02-23 19:46
kalle Yes 2015-02-23 04:46
kinncj Yes 2015-02-26 15:40
krakjoe Yes 2015-02-23 18:31
laruence No 2015-03-07 12:39
lcobucci Yes 2015-02-25 18:51
leigh Yes 2015-02-23 14:53
levim Yes 2015-02-23 04:32
lstrojny Yes 2015-03-02 13:15
mariuz Yes 2015-02-23 17:15
mbeccati Yes 2015-02-27 09:56
mfonda Yes 2015-03-09 16:44
mike Yes 2015-02-23 13:30
mrook Yes 2015-02-26 20:25
nikic Yes 2015-02-23 11:12
olemarkus Yes 2015-02-23 08:48
pajoye Yes 2015-02-23 05:28
patrickallaert Yes 2015-03-02 11:24
pauloelr Yes 2015-02-23 16:18
peehaa Yes 2015-02-26 17:24
philstu Yes 2015-02-23 15:54
ralphschindler Yes 2015-03-07 23:04
rdlowrey Yes 2015-02-23 16:20
rdohms Yes 2015-02-24 10:44
remi No 2015-02-23 07:01
salathe Yes 2015-02-27 20:06
santiagolizardo Yes 2015-02-26 11:51
sobak Yes 2015-02-27 04:33
stas No 2015-02-24 07:47
thekid Yes 2015-02-28 16:13
treffynnon Yes 2015-02-23 12:33
tyrael Yes 2015-02-23 10:01
yohgaki Yes 2015-02-23 06:21
yunosh Yes 2015-02-23 09:45
zeev Yes 2015-03-02 14:15

Discussions on the mailing list

[RFC] Remove PHP 4 Constructors
by Levi Morrison · 96 messages
[RFC] Remove PHP 4 Constructors Update
by Levi Morrison · 4 messages
[RFC] Deprecate properties using var
by Pierre du Plessis · 6 messages
[VOTE] Remove PHP 4 Constructors
by Levi Morrison · 5 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

PHP 4 constructors are methods that have the same name as the class they are defined in. PHP 5 preserved the ability to use PHP 4 style constructors in some cases; the strangeness of when the constructor is and isn't used increases the mental model for programmers.

class Filter {
 
    // A constructor in PHP 4 and 5
    function filter($a) {}
}
// Namespaced classes do not recognize PHP 4 constructors
namespace NS;
class Filter {
 
    // Not a constructor
    function filter($a) {}
}
// Defining __construct and filter makes filter a normal method
class Filter {
    function __construct() {}
 
    // Not a constructor
    function filter($a) {}
}
 
//But defining filter first raises an E_STRICT
class Filter {
 
    // Not a constructor…
    function filter($a) {}
 
    // This raises E_STRICT
    function __construct() {}
}

Proposal

PHP 7 will emit E_DEPRECATED whenever a PHP 4 constructor is defined. When the method name matches the class name, the class is not in a namespace, and a PHP 5 constructor (__construct) is not present then an E_DEPRECATED will be emitted. PHP 8 will stop emitting E_DEPRECATED and the methods will not be recognized as constructors.

PHP 7 will also stop emitting E_STRICT when a method with the same name as the class is present as well as __construct.

Refer to the Examples section to see how code is impacted.

Backward Incompatible Changes

Since an E_DEPRECATED will be emitted in PHP 7 there may be some backwards compatibility breaks when people use custom error handlers.

In PHP 8 recognition for old constructors will be outright removed, meaning that anything without a __construct will not work the same way it used to. The old-style constructor will be considered a normal method and will not be called when the object is constructed. The fix is to rename the constructor to __construct.

Examples

class Filter {
 
    // PHP 5: filter is a constructor
    // PHP 7: filter is a constructor and E_DEPRECATED is raised
    // PHP 8: filter is a normal method and is not a constructor; no E_DEPRECATED is raised
    function filter($a) {}
}
 
$filter = new ReflectionMethod('Filter', 'filter');
 
// PHP 5: bool(true)
// PHP 7: bool(true)
// PHP 8: bool(false)
var_dump($filter->isConstructor());
// function filter is not used as constructor in PHP 5+
class Filter {
    // PHP 5: E_STRICT "Redefining already defined constructor"
    // PHP 7: No error is raised
    // PHP 8: No error is raised
    function filter($a) {}
    function __construct() {}
}
// function filter is not used as constructor in PHP 5+
class Filter {
    function __construct() {}
 
    // PHP 5.0.0 - 5.2.13, 5.3.0 - 5.3.2: E_STRICT "Redefining already defined constructor"
    // PHP 5.2.14 - 5.2.17, 5.3.3 - 5.6: No error is raised
    // PHP 7: No error is raised
    // PHP 8: No error is raised
    function filter($a) {}
}
class Filter {
    // PHP 5: filter is a constructor
    // PHP 7: filter is a constructor and E_DEPRECATED is raised
    // PHP 8: filter is a normal method and is not a constructor; no E_DEPRECATED is raised
    function filter() {}
}
 
class FilterX extends Filter {
 
    function __construct() {
        // PHP 5: Filter::filter is called; no error
        // PHP 7: Filter::filter is called; no error
        // PHP 8: "Fatal error: Cannot call constructor"
        parent::__construct();
    }
 
}
 
new FilterX();

Voting

This RFC targets PHP 7 and PHP 8. Please read the RFC to understand what is being proposed.

This RFC requires 2/3 vote in favor of deprecating and removing PHP 4 style constructors to pass.

Do you vote to remove PHP 4 constructors as outlined in this RFC?

Vote: remove_php4_constructors — Yes: 50 · No: 4 (closed)

Voting will close on the evening (UTC-7) of March 6th.

Patches and Tests

An implementation based on the master branch can be found here: https://github.com/php/php-src/pull/1061