php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #17637 constructors in classes (Back to PHP3)
Submitted: 2002-06-07 05:32 UTC Modified: 2003-08-10 18:26 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: Arvid at knast dot dk Assigned:
Status: Closed Package: Class/Object related
PHP Version: 4.2.1 OS: tested on FreeBSD and WinXP
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: Arvid at knast dot dk
New email:
PHP Version: OS:

 

 [2002-06-07 05:32 UTC] Arvid at knast dot dk
first tjek the links, and you are maybe able to see the bug:
http://www.php.net/manual/en/language.oop.constructor.php
http://www.bottiger.geeknet.dk/misc/objekt.php

if not, i will try to eksplain it, but my english sucks ;)
<quote from="http://www.php.net/manual/en/language.oop.constructor.php">
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>'. 
</quote>

This isn't working in PHP 4.2.1 (or 4.2 i think).

When you call the classA is A() a constructor and it prints out 'I am the constructor of A.<br>\n'.
That works fine :D

But there is a problem by calling ClassB.
ClassB extends ClassA, and try listen to the rule one's again:
'constructor is a function of the same name as the class it is being defined in.'
There is no function called B() ind ClassB, sp ClassB has NO constructors!

In ClassA who extends in ClassB we have to functions, A() and B().
A() is the constructor of ClassA, and should be printed out when we call ClassB, but is dosn't.
Instead of printing out A() PHP 4.2.1 has the same bug as PHP3, it prints out B(), and that is the total opposite to what the rule say's




Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-07 06:18 UTC] sander@php.net
OK, the point is that a function is a constructor if it has the same name as the class, regardless it's been defined in another class.
Either the docs are wrong or this is a bug.
 [2003-08-10 17:55 UTC] helly@php.net
PHP 5 modell does this correect.
 [2003-08-10 18:26 UTC] sniper@php.net
Fixed in PHP 5.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 13:01:30 2024 UTC