php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #22253 method becomes constructor in subclass
Submitted: 2003-02-17 11:38 UTC Modified: 2005-04-06 11:10 UTC
Votes:3
Avg. Score:2.7 ± 1.2
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:0 (0.0%)
From: goba@php.net Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.2-dev OS: win2k
Private report: No CVE-ID: None
 [2003-02-17 11:38 UTC] goba@php.net
In this example, the printStr() method becomes the constructor of the printStr class, while I think it should not be working this way... I hope this is not by design ;)

class String
{
    function printStr($string)
    {
        print $string;
    }
}
class printStr extends String {}
$ps = new printStr("abc");

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-21 17:37 UTC] andrew at evilwalrus dot com
According to the comments on the OOP manual page, if a constructor is not located in the base class, the function of the same name will be located in subsequent classes, and loaded accordingly.  Yes, this is by design, but no, i personally don't like it... correct me if i'm wrong, please.

~ Andrew Heebner
 [2003-02-23 01:20 UTC] sniper@php.net
It's by design and even documented here:
http://www.php.net/manual/en/language.oop.constructor.php

 [2003-02-23 05:56 UTC] goba@php.net
No, no and no.

Excerpts:

|In PHP 4, a function becomes a constructor,
|when it has the same name as the class it
|is defined in"

In my example, the function printStr is not defined in class printStr, so it should not become a constructor according to this statament.

Another example at the bottom of the page you mentioned:

|class A
|{
|    function A()
|    {
|        echo "I am the constructor of A.<br>\n";
|    }
|
|    function B()
|    {
|        echo "I am a regular function named B in class
|        A.<br>\n";
|        echo "I am not a constructor in A.<br>\n";
|    }
|}
|
|class B extends A
|{
|    function C()
|    {
|        echo "I am a regular function.<br>\n";
|    }
|}
|
|// This will call B() as a constructor.
|$b = new B;
|
|In PHP 3, the function B() in class A will suddenly become
|a constructor in class B, although it was never intended to 
|be. The rule in PHP 3 is: 'A constructor is a function of
|the same name as the class.'. PHP 3 does not care if the
|function is being defined in class B, or if it has been
|inherited.
|
|This is fixed in PHP 4 by modifying the rule to: 'A
|constructor is a function of the same name as the class
|it is being defined in.'. Thus in PHP 4, the class B
|would have no constructor function of its own and the
|constructor of the base class would have been called,
|printing 'I am the constructor of A.<br>'.

The above example says, that the B() method becomes a constructor in PHP 3, but *not* in PHP 4, as there is no constructor defined for class B itself. Therefore it inherits the base classes constructor, which exists in the manual's example. In my case, there is no constructor in the base class. Therefore it should not call any method, as the text suggests.

So this is not a documented behaviour. In fact it is documented, that it should not work this way in PHP 4, but only in PHP 3...
 [2003-02-23 17:14 UTC] sniper@php.net
Okay, now I got it. :)

The rule for PHP 4 should be:

"If class extending a base class hasn't got a constructor of it's own, then the constructor of the base class is called."

Your example lacked the constructor for class String itself..but the last example in manual has that special case. And running that example shows that this is bullshit:

"This is fixed in PHP 4 by modifying the rule to: 'A
constructor is a function of the same name as the class
it is being defined in.'. Thus in PHP 4, the class B
would have no constructor function of its own and the
constructor of the base class would have been called,
printing 'I am the constructor of A.<br>'."

(as it actually calls the method B()!)

p.s. Someone should clear out those PHP 3 examples and changes out of the docs altogether as they only cause confusion and only document how it should behave in PHP 4.


 [2003-02-24 02:53 UTC] zeev@php.net
This behavior will not be changed in the context of PHP 4.x, but will be fixed in PHP 5.0 (Zend Engine 2)
 [2003-02-24 07:59 UTC] goba@php.net
Reopening as a doc problem, so doc guys will get this to fix...
 [2005-04-06 11:10 UTC] vrana@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.

Old stuff removed, PHP 4 description fixed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 02:01:29 2024 UTC