php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #66926 old-style constructor and __construct()
Submitted: 2014-03-18 16:39 UTC Modified: 2015-09-03 22:17 UTC
Votes:2
Avg. Score:1.5 ± 0.5
Reproduced:1 of 2 (50.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: wako057 at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: Irrelevant OS: Debian
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: wako057 at gmail dot com
New email:
PHP Version: OS:

 

 [2014-03-18 16:39 UTC] wako057 at gmail dot com
Description:
------------
I've tested this issue on php 5.3.28 and php 5.5.10
When the parser found in class a method called as the class, insensitive to the case, it generate a Strict Standard error.

But if it found the __construct first, at this moment no error is raised.

With namespace the case doesn't happened at all.

Test script:
---------------
<?php
class Foo {

    public function foo() {
        echo 'ahaha';
    }
    function __construct() {
        echo 'hihi';
    }

}


Generate a: PHP Strict Standards:  Redefining already defined constructor for class Foo

BUT 
<?php
class Foo {

    function __construct() {
        echo 'hihi';
    }

    public function foo() {
        echo 'ahaha';
    }
}

Don't


<?php
namespace Bar;
class Foo {

    public function foo() {
        echo 'ahaha';
    }
    function __construct() {
        echo 'hihi';
    }

}

new Foo();
 
Neither


Expected result:
----------------
When a __construct is present, no error should be raised

Actual result:
--------------
Strict Standards: Redefining already defined constructor for class Foo in /var/www/tt.php on line 7


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-04-17 00:35 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2015-04-17 00:35 UTC] cmb@php.net
The reported behavior is to be expected, see the man page on
"Constructors and Destructors"[1].

You have filed this as "Missing Information in translation", so we
have to assume that there is a documentation translation problem.
It is not clear, however, which translation (i.e. language) is
affected. Can you please clarify?

[1] <http://php.net/manual/en/language.oop5.decon.php>
 [2015-04-17 08:41 UTC] wako057 at gmail dot com
-Status: Feedback +Status: Assigned
 [2015-04-17 08:41 UTC] wako057 at gmail dot com
Hello,

The qualification is maybe not the correct one, it can be a parser probleme.
My native language is French.

Assuming english is not my native one maybe im wrong, but I think both doc are inexact:
Doc:
For backwards compatibility, if PHP 5 cannot find a __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. 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.

So <<if PHP 5 cannot find>>
The problem describe here is php raised an error depending on which method the parser find first, not if it is present or not.


Regards.
 [2015-04-17 11:21 UTC] cmb@php.net
-Status: Assigned +Status: Verified -Type: Bug +Type: Documentation Problem -Package: Documentation problem +Package: Scripting Engine problem -Assigned To: cmb +Assigned To:
 [2015-04-17 11:21 UTC] cmb@php.net
> The problem describe here is php raised an error depending on
> which method the parser find first, not if it is present or not.

Thanks, now its clear to me what you are referring to. And indeed,
the documentation should be improved.
 [2015-08-18 13:38 UTC] cmb@php.net
-Summary: Missing Information in translation +Summary: old-style constructor and __construct() -Assigned To: +Assigned To: cmb
 [2015-08-18 13:41 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=337534
Log: if __construct() and old-style constructor are present, PHP chooses whichever
comes first (fixes #66926)
 [2015-08-18 13:41 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2015-08-18 13:41 UTC] cmb@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.
 [2015-09-03 21:21 UTC] miksir at maker dot ru
PHP5 always use __construct, not whichever comes first
https://3v4l.org/7eg0E
https://3v4l.org/K0FqA
 [2015-09-03 22:15 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=337740
Log: reverted erroneous fix for bug #66926
 [2015-09-03 22:17 UTC] cmb@php.net
-Status: Closed +Status: Not a bug
 [2015-09-03 22:17 UTC] cmb@php.net
Thanks for pointing out my mistake, miksir! :)

So actually, this is not a bug.
 [2015-09-04 11:59 UTC] miksir at maker dot ru
You are welcome =)

And to wako057:
It's not a bug, it's feature :) https://bugs.php.net/bug.php?id=52160
Emit E_STRICT in second case not possible because it's broke legal PHP5 code.
But in first case E_STRICT still emitted because very low chance that constructor is not first method in class.
So, this strange behavior - is the balance between legal PHP5 code, BC with PHP4 code and warning about BC break (about this one: "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-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 17:01:36 2024 UTC