php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34592 RNF: Configurable constructor lookup behavior
Submitted: 2005-09-21 23:46 UTC Modified: 2005-09-22 01:01 UTC
From: david at nap dot edu Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 5.0.5 OS: Redhat Linux / Fedora Core 3
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2005-09-21 23:46 UTC] david at nap dot edu
Description:
------------
I'd like a configuration option that would disable backward-compatible lookups of class constructor names (ie. methods named the same as the class).

I'm attempting to implement a Hibernate-like persistence layer for php objects.  To facilitate this, several things happen at object creation time, currently using the 'magic method' __construct in a persistent superclass.  However, I'd prefer to allow users to implement constructor methods named as the class; in this scenario my __construct() method would run and then use reflection to invoke the user constructor.  However, this is not possible as the presence of the class-named method currently prevents __construct  from being called.  

So, it would be useful to me to be able to switch off this behavior.  If there is interest in this but no time I'd be happy to provide a patch.

Reproduce code:
---------------
   class A {
     function __construct() {
       echo "A ctor\n";
     }
   }

   class B extends A {
     function B() {
       echo "B ctor\n";
     }
   }

   $foo = new B();



Expected result:
----------------
I'd like a php.ini option that would cause the result to be 

   A ctor

or even

   A ctor
   B ctor

if that is somehow more desirable.

Actual result:
--------------
Currently, the result is 

   B ctor


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-21 23:53 UTC] tony2001@php.net
>However, I'd prefer to allow users to implement 
>constructor methods named as the class;
Why not to override __construct() instead?

 [2005-09-22 00:05 UTC] david at nap dot edu
> Why not to override __construct() instead?

  I could specify that users of my system must write constructors that override __construct, however this has the disadvantage that they must remember to make the call to the superclass __construct before doing anything else (for example, assignments to class fields from a constructor is common, and these need to be caught and mark object as dirty).  I could also do something like specify that constructors in domain objects have an underscore prepended to the name, such as "function _MyClass() { ... }".  In this case if the underscore is forgotten, errors will ensue much later due to __construct not being called.  

Both of these seem less appealing than having users just write a ctor in the way they are used to (named as class), and having it work. 

In general, I feel there should be a way to hook object creation for non-end-user purposes (tracing, profiling, etc, as well) that is not disabled by the presence of a constructor written by the user who is not aware of the workings of the (persistence, tracing, profiling, etc) mechanism.
 [2005-09-22 01:01 UTC] tony2001@php.net
>however this has the disadvantage that they must
>remember to make the call to the superclass __construct 
>before doing anything else

Document it. It'll solve the problem.

I don't think that this deprecated feature will be supported even more in the future.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jul 03 09:01:29 2024 UTC