php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29689 default value of protected member overrides default value of private
Submitted: 2004-08-15 21:52 UTC Modified: 2005-06-09 19:22 UTC
Votes:10
Avg. Score:3.8 ± 1.2
Reproduced:8 of 9 (88.9%)
Same Version:0 (0.0%)
Same OS:1 (12.5%)
From: php dot net at benjamin dot schulz dot name Assigned: andi (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2005-04-19 OS: *
Private report: No CVE-ID: None
 [2004-08-15 21:52 UTC] php dot net at benjamin dot schulz dot name
Description:
------------
protected member overrides private

Reproduce code:
---------------
<?php

class foo {
    private $foo = 'foo';

    function printFoo()
    {
        echo __CLASS__, ': ', $this->foo, '<br />';
    }
}

class bar extends foo {
    protected $foo = 'bar';

    function printFoo()
    {
        parent::printFoo();
        echo __CLASS__, ': ', $this->foo, '<br />';
    }
}

class baz extends bar {
    protected $foo = 'baz';
}

$bar = new bar;
$bar->printFoo();
echo '<hr />';
$baz = new baz();
$baz->printFoo();
?>

Expected result:
----------------
foo: foo
bar: bar
--
foo: foo
bar: baz

Actual result:
--------------
foo: foo
bar: bar
--
foo: baz
bar: baz

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-15 22:03 UTC] php dot net at benjamin dot schulz dot name
default value of protected member overrides default value of private
 [2004-08-31 16:24 UTC] tony2001@php.net
Private members belong only to the class where they were defined and cannot be inherited.
 [2004-08-31 17:48 UTC] php dot net at benjamin dot schulz dot name
yep tony, you're right, and now read the bug report
 [2004-08-31 18:32 UTC] tony2001@php.net
Please, be more verbose next time if you want to get any help. Your conciseness doesn't help very much.
The problem is that you're trying to access class members through $this variable using static methods. As you can understand in this case there is no $this and behavior seems to be undefined (shouldn't there be an error, btw?).
 [2004-08-31 19:00 UTC] php dot net at benjamin dot schulz dot name
you don't get the point, the problem is that baz's protected $foo overrides foo's private $foo, that should not happen

using parent::foo() is a common technique, and there is a $this because it get's the context of the calling point
 [2005-04-19 16:19 UTC] sniper@php.net
<?php

class foo {
    private $foo = 'foo';

    function printFoo1()
    {
        echo __CLASS__, ': ', $this->foo, "\n";
    }
}

class bar extends foo {
    protected $foo = 'bar';

    function printFoo2()
    {
        echo __CLASS__, ': ', $this->foo, "\n";
    }
}

class baz extends bar {
    protected $foo = 'baz';

    function printFoo3()
    {
        echo __CLASS__, ': ', $this->foo, "\n";
    }
}
$bar = new baz;
$bar->printFoo1();
$bar->printFoo2();
$bar->printFoo3();
?>

Outputs:
foo: baz
bar: baz
baz: baz

When it should output:
foo: foo
bar: bar
baz: baz

Right?

 [2005-05-17 16:10 UTC] stas@php.net
No, I think it should output foo, baz, baz. Private property is to behave for external functions as if it did not exist at all. However, protected property is shared by the whole inheritance chain, so redefining it means redefining it everywhere. 
 [2005-06-09 19:22 UTC] stas@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.

fixed it
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 03:01:29 2024 UTC