# [RFC] Remove PHP 4 Constructors

- Thread: https://staging.externals.io/message/78969
- Original archive: https://news-web.php.net/php.internals/78969
- Started by Levi Morrison on 2014-11-18T23:11:17+00:00
- Messages: 96
- RFC: Remove PHP 4 Constructors (Implemented (PHP 7.0)) — https://staging.externals.io/rfc/remove_php4_constructors.md
- Page 1 of 4: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://staging.externals.io/message/78969.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://staging.externals.io/search.md?q={query} · RFCs at https://staging.externals.io/rfc.md · JSON API at https://staging.externals.io/api · documentation at https://staging.externals.io/llms.txt

---

## #78969 — Levi Morrison — 2014-11-18T23:11:17+00:00

URL: https://staging.externals.io/message/78969#78969

Dear Internals,

I am proposing an RFC[1] to remove PHP 4 constructors in PHP 7. If
accepted, methods with the same name as their defining class will no
longer be recognized as constructors. As noted in the RFC, there are
already many situations where we do not recognize these methods as
constructors, such as in namespaces and traits and when `function
__construct` is also present.

Andrea Faulds has kindly written a utility that identifies when a PHP
4 constructor is defined[2]. It does not automatically change the code
for liability reasons. The utility PHPMD[3] can also detect this but
has a false positive when `__construct` is also defined.

Cheers,
Levi Morrison


  [1]: https://wiki.php.net/rfc/remove_php4_constructors
  [2]: https://github.com/TazeTSchnitzel/PHP4_Constructor_Finder
  [3]: http://phpmd.org/rules/naming.html#constructorwithnameasenclosingclass

---

## #78972 — Julien Breux — 2014-11-18T23:26:51+00:00

URL: https://staging.externals.io/message/78969#78972 · In reply to: #78969

I think that it is great time to end to PHP 4 constructors system for PHP 7.

IMO, It's a good RFC.




> Dear Internals,
>
> I am proposing an RFC[1] to remove PHP 4 constructors in PHP 7. If
> accepted, methods with the same name as their defining class will no
> longer be recognized as constructors. As noted in the RFC, there are
> already many situations where we do not recognize these methods as
> constructors, such as in namespaces and traits and when `function
> __construct` is also present.
>
> Andrea Faulds has kindly written a utility that identifies when a PHP
> 4 constructor is defined[2]. It does not automatically change the code
> for liability reasons. The utility PHPMD[3] can also detect this but
> has a false positive when `__construct` is also defined.
>
> Cheers,
> Levi Morrison
>
>
>   [1]: https://wiki.php.net/rfc/remove_php4_constructors
>   [2]: https://github.com/TazeTSchnitzel/PHP4_Constructor_Finder
>   [3]:
> http://phpmd.org/rules/naming.html#constructorwithnameasenclosingclass

---

## #78979 — Kris Craig — 2014-11-19T03:46:35+00:00

URL: https://staging.externals.io/message/78969#78979 · In reply to: #78972

On Tue, Nov 18, 2014 at 3:26 PM, Julien Breux <julien.breux@gmail.com>
wrote:

> I think that it is great time to end to PHP 4 constructors system for PHP
> 7.
>
> IMO, It's a good RFC.
>

Agreed.  I was going to suggest we throw `E_DEPRECATED` for 5.x, but you
already have that covered in the RFC.  I don't see anything wrong with this
proposal, in fact.  In hindsight, it probably would've been better if we
had started raising `E_DEPRECATED` for it back in 5.0, but better now than
never.  We certainly have no obligation to support BC on features that
became obsolete with PHP 4.

+1 on this.

--Kris


>
> 
>
> > […]

---

## #78980 — Yasuo Ohgaki — 2014-11-19T04:15:30+00:00

URL: https://staging.externals.io/message/78969#78980 · In reply to: #78969

Hi all,



> I am proposing an RFC[1] to remove PHP 4 constructors in PHP 7. If
> accepted, methods with the same name as their defining class will no
> longer be recognized as constructors. As noted in the RFC, there are
> already many situations where we do not recognize these methods as
> constructors, such as in namespaces and traits and when `function
> __construct` is also present.
>
> Andrea Faulds has kindly written a utility that identifies when a PHP
> 4 constructor is defined[2]. It does not automatically change the code
> for liability reasons. The utility PHPMD[3] can also detect this but
> has a false positive when `__construct` is also defined.
>

No reason to keep old constructor for PHP7. IMHO.
+1 for removing it PHP7, announce depreciation now.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #78981 — Julien Pauli — 2014-11-19T08:30:44+00:00

URL: https://staging.externals.io/message/78969#78981 · In reply to: #78980

> Hi all,
>
> 
>
> > […]
>
> No reason to keep old constructor for PHP7. IMHO.
> +1 for removing it PHP7, announce depreciation now.

This is obviously a +1.

Julien.Pauli

---

## #78983 — Patrick ALLAERT — 2014-11-19T08:45:32+00:00

URL: https://staging.externals.io/message/78969#78983 · In reply to: #78981

Le Wed Nov 19 2014 at 9:31:50 AM, Julien Pauli <jpauli@php.net> a écrit :

> 
> > […]
>
> This is obviously a +1.
>
> Julien.Pauli
>

+1

---

## #78984 — Ferenc Kovacs — 2014-11-19T08:46:01+00:00

URL: https://staging.externals.io/message/78969#78984 · In reply to: #78969

> Dear Internals,
>
> I am proposing an RFC[1] to remove PHP 4 constructors in PHP 7. If
> accepted, methods with the same name as their defining class will no
> longer be recognized as constructors. As noted in the RFC, there are
> already many situations where we do not recognize these methods as
> constructors, such as in namespaces and traits and when `function
> __construct` is also present.
>
> Andrea Faulds has kindly written a utility that identifies when a PHP
> 4 constructor is defined[2]. It does not automatically change the code
> for liability reasons. The utility PHPMD[3] can also detect this but
> has a false positive when `__construct` is also defined.
>
> Cheers,
> Levi Morrison
>
>
>   [1]: https://wiki.php.net/rfc/remove_php4_constructors
>   [2]: https://github.com/TazeTSchnitzel/PHP4_Constructor_Finder
>   [3]:
> http://phpmd.org/rules/naming.html#constructorwithnameasenclosingclass
>
> --
> 
> 
>
>
+1 from me.

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #78991 — Remi Collet — 2014-11-19T13:41:54+00:00

URL: https://staging.externals.io/message/78969#78991 · In reply to: #78969

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Le 19/11/2014 00:11, Levi Morrison a écrit :
> [1]: https://wiki.php.net/rfc/remove_php4_constructors

This will just kill PEAR and most of available libraries on PEAR forge.

Yes they are still some use of them
Yes keeping PHP 4 for PEAR is a bad idea


Remi.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlRsniIACgkQYUppBSnxahilDwCgtcsNH9UtJFtkVkY0KwqxRiZn
oZgAoLzKJ4zbO6mBerkivLziwFahOAF/
=pQ60
-----END PGP SIGNATURE

---

## #78993 — Maxime Veber — 2014-11-19T13:53:37+00:00

URL: https://staging.externals.io/message/78969#78993

Outch, I didn't know that this use case was still valid. Yes, ofc, +1.

2014-11-19 14:46 GMT+01:00 Alain Williams <addw@phcomp.co.uk>:

> 
> > […]
>
> Maybe this would spur PEAR to moving to a modern PHP, they could always
> maintain
> a separate PEAR legacy. PHP 4 support ended some 6 years ago.
>
> --
> Alain Williams
> Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT
> Lecturer.
> +44 (0) 787 668 0256  http://www.phcomp.co.uk/
> Parliament Hill Computers Ltd. Registration Information:
> http://www.phcomp.co.uk/contact.php
> #include <std_disclaimer.h

---

## #78995 — Alain Williams — 2014-11-19T14:33:29+00:00

URL: https://staging.externals.io/message/78969#78995

> PEAR is not a single organisation who can mass update all the
> modules; the guidelines could be updated, if they haven't been
> already, but there would still be a whole repository full of
> libraries which used this.
> 
> Now, whether that's acceptable or not, I don't know, but it does
> highlight the size of the compatibility break.

How many servers are stuck on PHP 4 ?

Of those 'stuck' servers, how many have applications still under active
development ?

The point is: how many people would get annoyed if PEAR stopped supporting PHP 4 ?

IMHO: making PHP 5.3+ the PEAR baseline would not seem unreasonable.

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h

---

## #78996 — Andrey Andreev — 2014-11-19T14:36:32+00:00

URL: https://staging.externals.io/message/78969#78996 · In reply to: #78995

Hi,


> 
>
> > […]
>
> How many servers are stuck on PHP 4 ?
>
> Of those 'stuck' servers, how many have applications still under active
> development ?
>
> The point is: how many people would get annoyed if PEAR stopped supporting PHP 4 ?
>
> IMHO: making PHP 5.3+ the PEAR baseline would not seem unreasonable.

Nope. The point is, how much of that code *depends on PHP4-style
constructors*, which work perfectly fine in PHP 5.x.

Regardless, it's time to put it in the past, so a +1 from me for this RFC.

Cheers,
Andrey.

---

## #78998 — Chris Wright — 2014-11-19T14:41:09+00:00

URL: https://staging.externals.io/message/78969#78998

> Alain Williams wrote on 19/11/2014 13:46:
>
> > […]
> PEAR is not a single organisation who can mass update all the modules; the
> guidelines could be updated, if they haven't been already, but there would
> still be a whole repository full of libraries which used this.
>
> Now, whether that's acceptable or not, I don't know, but it does highlight
> the size of the compatibility break.
>

It would be quite easy to write a tool to detect classes that use the old
style, and even automatically fix them in most cases. As BC breaks go, this
is about as minimal as it gets for package maintainers.

Note that, for users who are insane enough to expect to maintain PHP4-7
support in a single codebase, it's also easily possible to work with both
styles even after this change is introduced:

class Foo
{
    function Foo()
    {
        // invoked as the ctor in PHP4
        $this->__construct();
    }

    function __construct()
    {
        // invoked as the ctor in PHP5/7
    }
}

---

## #79000 — Alain Williams — 2014-11-19T15:07:13+00:00

URL: https://staging.externals.io/message/78969#79000 · In reply to: #78998

> Note that, for users who are insane enough to expect to maintain PHP4-7
> support in a single codebase, it's also easily possible to work with both
> styles even after this change is introduced:
> ...

It is a problem trying to maintain code for different versions of PHP,
especially where there are syntax differences. It would be really nice to have
some sort of conditional compilation as in C. Eg:

It would be nice to be able to do something like:

try {
 ....

# if `PHP_VERSION_ID` > 50500
} catch(PDOException $e) {
  ....
} finally {
   ... tidy up
}
# else
} catch(PDOException $e) {
  ... tidy up not quite where I want it
  ....
}
# endif

OK: '#' might not be a good character since it is start of comment, but that is
the idea.

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h

---

## #79001 — Andrea Faulds — 2014-11-19T15:10:27+00:00

URL: https://staging.externals.io/message/78969#79001 · In reply to: #79000

> 
> 
> It is a problem trying to maintain code for different versions of PHP,
> especially where there are syntax differences. It would be really nice to have
> some sort of conditional compilation as in C. Eg:
> 
> It would be nice to be able to do something like:
> 
> try {
> ....
> 
> # if `PHP_VERSION_ID` > 50500
> } catch(PDOException $e) {
>  ....
> } finally {
>   ... tidy up
> }
> # else
> } catch(PDOException $e) {
>  ... tidy up not quite where I want it
>  ....
> }
> # endif
> 
> OK: '#' might not be a good character since it is start of comment, but that is
> the idea.

You could actually run the C preprocessor on your PHP codebase if you wanted!

But yes, I can see there might be a need for conditional compilation. To a certain extent we already have this, in that you can conditionally define functions and classes. Perhaps this could be extended? Conditionally defining methods?

    class FooBar {
        if (PHP_VERSION_ID < 50000) {
            public function FooBar() {
                $this->__construct();
            }
        }

        public function __foobar() {
            
        }
    }
--
Andrea Faulds
http://ajf.me/

---

## #79003 — Ralf Lang — 2014-11-19T16:32:25+00:00

URL: https://staging.externals.io/message/78969#79003 · In reply to: #79000

> 
> 
> 
> > […]
> 
> It is a problem trying to maintain code for different versions of PHP,
> especially where there are syntax differences.

Not in this case. Old PHP 4 code (regarding constructors) works through
all PHP 5.x - New PHP 5 code has been working for the last 6 or 7 years
worth of PHP releases and will continue to work. Code which has
duplicate constructors (or one calling the other) will work on both
platforms (only regarding constructors).

+1 for the change even though or because it will separate some of the
PEAR heritage from modern platforms if unchanged/unmaintained.


-- 
Ralf Lang
Linux Consultant / Developer
Tel.: +49-170-6381563
Mail: lang@b1-systems.de
B1 Systems GmbH
Osterfeldstraße 7 / 85088 Vohburg / http://www.b1-systems.de
GF: Ralph Dehner / Unternehmenssitz: Vohburg / AG: Ingolstadt,HRB 3537

---

## #79004 — Ferenc Kovacs — 2014-11-19T16:37:12+00:00

URL: https://staging.externals.io/message/78969#79004 · In reply to: #78995

> 
>
> > […]
>
> How many servers are stuck on PHP 4 ?
>
> Of those 'stuck' servers, how many have applications still under active
> development ?
>
> The point is: how many people would get annoyed if PEAR stopped supporting
> PHP 4 ?
>
> IMHO: making PHP 5.3+ the PEAR baseline would not seem unreasonable.
>
>
There were already discussion about bumping the php requirements for the
next PEAR package:
http://comments.gmane.org/gmane.comp.php.pear.devel/50823
But what it is more important is that even if some PEAR packages will be
getting new releases with modern/refactored APIs with bumped major
versions, that will still not break the already existing installation, and
people will still be able to install the latest package version which
supports their php version (and it would be still possible to continue
releasing patch versions for security updates for the php4 compatible
versions).
So while I agree that PEAR in it's current form will be the biggest public
repository with code affected by this change, it isn't like they can't move
forward or that users using PEAR for their projects will be left without an
upgrade path and plenty of time to execute it.

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #79005 — Julien Pauli — 2014-11-19T18:17:48+00:00

URL: https://staging.externals.io/message/78969#79005 · In reply to: #79001

>
> > […]
>
> You could actually run the C preprocessor on your PHP codebase if you wanted!
>
> But yes, I can see there might be a need for conditional compilation. To a certain extent we already have this, in that you can conditionally define functions and classes. Perhaps this could be extended? Conditionally defining methods?
>
>     class FooBar {
>         if (PHP_VERSION_ID < 50000) {
>             public function FooBar() {
>                 $this->__construct();
>             }
>         }
>
>         public function __foobar() {
>
>         }
>     }
> --

[Off Topic]

It's been a long time I've been thinking about having a compile-time
preprocessor integrated into our parser/compiler stack.
I would design it just to support #migration tokens, and nothing more.
This would really ease migrations, with no runtime impact at all, and
the possibility to use any syntax, knowing it will get compiled, which
is not possible nowadays.
Like the C preprocessor. And it wouldn't be that hard to add to our
codebase, as there should not be any runtime impact, just a two-pass
parser

Anyone interested can contact me :-)

[/Off Topic]


Julien.P

---

## #79029 — johannes@schlueters.de — 2014-11-20T09:43:48+00:00

URL: https://staging.externals.io/message/78969#79029 · In reply to: #78995

> How many servers are stuck on PHP 4 ?
> 
> Of those 'stuck' servers, how many have applications still under active
> development ?
> 
> The point is: how many people would get annoyed if PEAR stopped supporting PHP 4 ?

The point about breaking this is *not* PHP 4 compatibility but
compatibility between PECL packages. The name of the constructor method
is part of the API.

Think about code like this in module A:

   class A_class {
       function A_class() { }
   }

and then module B extending this:

   class B_class  extends A_class {
       function B_class() {
           A_class();
       }
   }

I also wonder how Andrea's tool handles more indirect cases (C_class
extends B_class, while B_class has no ctor so C_class calls directly
A_class's ctor)

So I'm -1 on this.

johannes

---

## #79039 — Xinchen Hui — 2014-11-20T14:25:00+00:00

URL: https://staging.externals.io/message/78969#79039 · In reply to: #79029

On Thu, Nov 20, 2014 at 5:43 PM, Johannes Schlüter
<johannes@schlueters.de> wrote:
> 
> > […]
>
> The point about breaking this is *not* PHP 4 compatibility but
> compatibility between PECL packages. The name of the constructor method
> is part of the API.
>
> Think about code like this in module A:
>
>    class A_class {
>        function A_class() { }
>    }
>
> and then module B extending this:
>
>    class B_class  extends A_class {
>        function B_class() {
>            A_class();
>        }
>    }
>
> I also wonder how Andrea's tool handles more indirect cases (C_class
> extends B_class, while B_class has no ctor so C_class calls directly
> A_class's ctor)
>
> So I'm -1 on this.
I am with you here.

leave it there doesn't hurt anybody.  but remove it will. why we need to ?

thanks
>
> johannes
>
>
>
> --
> 
> 
>



-- 
Xinchen Hui
@Laruence
http://www.laruence.com/

---

## #79040 — Andrey Andreev — 2014-11-20T14:32:26+00:00

URL: https://staging.externals.io/message/78969#79040 · In reply to: #79039

Hi,


>
> leave it there doesn't hurt anybody.  but remove it will. why we need to ?
>

Leaving it does hurt. Most developers with no PHP4 experience don't
know that such a feature exists and spend hours trying to figure out
why a parent class' constructor isn't getting called when it's not
overriden by self::__construct().

PHP can't support everything forever, old and nowadays rarely used
syntax like this one must go away.

Cheers,
Andrey.

---

## #79041 — Xinchen Hui — 2014-11-20T15:07:45+00:00

URL: https://staging.externals.io/message/78969#79041 · In reply to: #79040

> Hi,
>
> 
> > […]
>
> Leaving it does hurt. Most developers with no PHP4 experience don't
> know that such a feature exists and spend hours trying to figure out
> why a parent class' constructor isn't getting called when it's not
> overriden by self::__construct().
I really doubt "most" here.

developers with c++ experience must feel it's very friendly.

thanks
>
> PHP can't support everything forever, old and nowadays rarely used
> syntax like this one must go away.
>
> Cheers,
> Andrey.



-- 
Xinchen Hui
@Laruence
http://www.laruence.com/

---

## #79042 — Levi Morrison — 2014-11-20T16:11:35+00:00

URL: https://staging.externals.io/message/78969#79042 · In reply to: #79029

On Thu, Nov 20, 2014 at 2:43 AM, Johannes Schlüter
<johannes@schlueters.de> wrote:
> 
> > […]
>
> The point about breaking this is *not* PHP 4 compatibility but
> compatibility between PECL packages. The name of the constructor method
> is part of the API.
>
> Think about code like this in module A:
>
>    class A_class {
>        function A_class() { }
>    }
>
> and then module B extending this:
>
>    class B_class  extends A_class {
>        function B_class() {
>            A_class();
>        }
>    }
>
> I also wonder how Andrea's tool handles more indirect cases (C_class
> extends B_class, while B_class has no ctor so C_class calls directly
> A_class's ctor)
>
> So I'm -1 on this.

I just want to make sure I understand you correctly: you are saying
you are voting no on this RFC because a tool, which is not part of
this RFC but we kindly provide, doesn't detect when a certain thing is
called?

---

## #79044 — johannes@schlueters.de — 2014-11-20T17:00:19+00:00

URL: https://staging.externals.io/message/78969#79044 · In reply to: #79042

> > […]
> 
> I just want to make sure I understand you correctly: you are saying
> you are voting no on this RFC because a tool, which is not part of
> this RFC but we kindly provide, doesn't detect when a certain thing is
> called?

It is a non-trivial change. Fixing this is not always as some people
might suggest. If we like it or not there is tons of code out there
depending on this feature. Breaking code, which worked for 15 years,
where we even distribute such code in our main distribution (pear.phar
is full of it) is not an option.

And it is not only PHP - just looked at wordpress and there are quite a
few classes in there where PHP 4 style ctors are used.and which aren't
marked final/public and as such are part of the API, meaning that WP
modules might depend on that. Consequence there is that once the feature
is removed WP users need a new major WP version and new versions of
modules before they can upgrade. This will lead to very slow adoption of
a new PHP version.

We can deprecate it in 7 (and fix code in our distribution) and then
take a next step in a later version, though. This gives folks like WP
time to update their codebase and be ready when our larer version comes
out.

johannes

---

## #79045 — Rowan Collins — 2014-11-20T17:33:07+00:00

URL: https://staging.externals.io/message/78969#79045 · In reply to: #79044

Johannes Schlüter wrote on 20/11/2014 17:00:
> We can deprecate it in 7 (and fix code in our distribution) and then
> take a next step in a later version, though. This gives folks like WP
> time to update their codebase and be ready when our larer version comes
> out.

+1 for officially deprecating this for a while before removing; 
currently, there's not even an `E_STRICT` when defining or using old-style 
constructors. The manual isn't particular clear either:

 > For backwards compatibility, if PHP 5 cannot find a __construct() 
<http://php.net/manual/en/language.oop5.decon.php#object.construct> 
function for a given class, and the class did not inherit one from a 
parent class, it will search for the old-style constructor function, by 
the name of the class.

I notice the RFC does propose deprecation, but in 5.7; that's predicated 
on a 5.7 being released, which I know some people are not in favour of. 
Has there been a formal vote on whether 5.7 should exist as a 
lightweight "last 5.x" shortly before 7.x?

Even with that, there is the question of whether 5.7 would give users 
long enough to make sure everything was compatible with this change.

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #79051 — Levi Morrison — 2014-11-20T20:54:00+00:00

URL: https://staging.externals.io/message/78969#79051 · In reply to: #79044

> > […]
>
> It is a non-trivial change. Fixing this is not always as some people
> might suggest.

1) Identify PHP 4 constructors using one of several tools (including
upgrading to PHP 5.7 and getting E_DEPRECATEDs).
2) Use one of the several tools that support method rename refactoring
(Netbeans, PhpStorm, and others) to rename the methods to __construct.

You could probably automate it with a very high degree of success; I
just don't want to automatically change code for liability reasons.  I
think you are exaggerating the required work given the tools we have
at our disposal…

…but that's okay. You can think and vote however you chose.

> We can deprecate it in 7 (and fix code in our distribution) and then
> take a next step in a later version, though.

I am proposing `E_DEPRECATED` in PHP 5.7, just as the RFC for using
multiple default statements in switches (which was accepted, by the
way).

Updating to PHP 5.7 first gives you more time to prepare for this and
other BC breaks for PHP 7. There are already a few, and I suspect
we'll have a few more before it's shipped.

---

Next page: https://staging.externals.io/message/78969.md?page=2
