php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39127 Class-name constructor fallbacks produce strange results
Submitted: 2006-10-11 18:05 UTC Modified: 2008-03-17 14:56 UTC
Votes:3
Avg. Score:3.3 ± 1.2
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: colder@php.net Assigned: tony2001 (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.2.0RC5 OS: Irrelevant
Private report: No CVE-ID: None
 [2006-10-11 18:05 UTC] colder@php.net
Description:
------------
In php's OOP implementation, the "class-name method" fallback used when a __construct() method fails seems to have strange effects. Non-existant methods seems to be callable. It only happens if no constructor is defined in class b.

This can be reproduced in php4.4, 5.1, 5.2-cvs, 6-cvs :



Reproduce code:
---------------
class a { function a($arg='') { echo $arg; } } 
class b extends a {} 

$b = new b; 
$b->b('foo');
$b->__construct('foo');

Expected result:
----------------
Fatal error: Call to undefined method b::b() in ...
Fatal error: Call to undefined method b::__construct() in ...

Actual result:
--------------
foo
Fatal error: Call to undefined method b::__construct() 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-11 18:59 UTC] helly@php.net
expected behavior, b inherits a's constuctor and a::__construct obviously does not exist
 [2006-10-11 18:59 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

see above
 [2006-10-11 19:00 UTC] tony2001@php.net
From what I see, the following happens:
1) class_entry of "A" uses method a() as constructor;
2) class "B" doesn't have a constructor; 
3) do_inherit_parent_constructor() looks for "__construct" in "A"; 
4) there is no "__construct" in "A"; 
5) named constructor a() is added to "B" as b().

5) seems to be wrong to me, here is the patch:
http://tony2001.phpclub.net/dev/tmp/bug39127.diff

With the patch method a() is still callable and used as constructor in "B", but there would be no method b().
 [2006-10-12 08:01 UTC] judas dot iscariote at gmail dot com
This is indeed a misbehaviuor, in whatever case you decide to fix or not, it must be added to the docs.

"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."

docs does not say it will magically add a method named as the **child** class **to** the child class...
 [2006-10-12 13:10 UTC] tony2001@php.net
Dmitry added a valid point that someone might be relying on the code below to work:
class a { function a() { var_dump("a::a()");} }
class b extends a {}
class c extends b { 
function C() {
  B::b(); //or parent::b();
  /* ... */
}
}

This made me think that B::b() should be still callable statically (and be an alias for A::a()), since it's used as constructor.
Updated version of the patch is here:
http://tony2001.phpclub.net/dev/tmp/bug39127_1.diff
 [2006-10-12 13:58 UTC] colder@php.net
in that case, should parent::__construct() also be callable ? 
 [2006-10-12 14:00 UTC] tony2001@php.net
No, I don't think so.
 [2008-03-17 14:56 UTC] tony2001@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 15:01:33 2024 UTC