Bug #50892 [Com]: Protected members of non-congruent children classes are visible

From: Date: Wed, 22 Feb 2017 09:32:34 +0000
Subject: Bug #50892 [Com]: Protected members of non-congruent children classes are visible
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207505@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=50892&edit=1

 ID:                 50892
 Comment by:         mail at pmmaga dot net
 Reported by:        EdwardDrapkin at gmail dot com
 Summary:            Protected members of non-congruent children classes
                     are visible
 Status:             Open
 Type:               Bug
 Package:            Class/Object related
 Operating System:   Irrelevant
 PHP Version:        5.3.1
 Block user comment: N
 Private report:     N

 New Comment:

A PR was prepared to fix this on the current master (7.2) but it was decided the the potential BC
break could possibly be too much for a minor and it was suggested that it should wait for the next
major.


Previous Comments:
------------------------------------------------------------------------
[2016-03-27 16:16:02] nikic@php.net

Related To: Bug #69766

------------------------------------------------------------------------
[2015-09-08 00:38:08] cmb@php.net

| There is no design ground to accept that, it is a bug. The
| question that remains is: is it worth to fix BC-wise?

IMHO the question is rather: when is the BC break acceptable? 7.1
or 8.0?

------------------------------------------------------------------------
[2015-03-10 08:53:17] arth dot inbox at gmail dot com

normandiggs, your sample is irrelevant.
It's ok to access private and protected properties from same type object.
It's bad to access protected properties from different type object.

Why I hate this behavior:
http://3v4l.org/tiOC5 - we receive protected value
http://3v4l.org/uT9PC - we receive fatal error

We should not to rely to implementation of B class (see fiddles)

Another note - this behavior was broken in 5.2 version.

------------------------------------------------------------------------
[2011-02-16 12:17:42] normandiggs at gmail dot com

Another one example, even without "extends":

class Page_Element
{
        protected $name = 'name*';

        public function __construct(Page_Element $child = null)
        {
            echo $this->name;
            echo $child->name;
        }

}

new Page_Element(new Page_Element(null));

(from http://youtrack.jetbrains.net/issue/WI-4663)

So why $child->name can be accessed? It's new object!

------------------------------------------------------------------------
[2010-05-06 21:58:21] php at b8r dot org

Our problem differs slightly from this issue, but I think they share a root cause.  Here's a
sample of code with the expected outcome.
abstract class A {
    /**
     * undocumented function
     *
     * @return void
     */
    public function __get($propertyName)
    {
        $val = $this->$propertyName;
        echo "Current Value of '{$propertyName}' is '{$val}'\n";
        return $val;
    }
    
    public function __set($propertyName, $propertyValue)
    {
        echo "Setting Property '{$propertyName}'  to
'{$propertyValue}'\n";
        $this->$propertyName = $propertyValue;
    }
}

class B extends A {
    protected $name;
    
    public function populateName($val) {
        $a = 'name';
        $this->$a = $val;
    }
    
    public function testit() {
        $b = new B();
        $b->name = 'internal';
        $b->name;
    }
}

$two = new B();
$two->name = 'external';
$two->name;

$two->testit();

Expected Results:
-----------------
Setting Property 'name'  to 'external'
Current Value of 'name' is 'external'
Setting Property 'name'  to 'internal'
Current Value of 'name' is 'internal'

Actual Results:
----------------
Setting Property 'name'  to 'external'
Current Value of 'name' is 'external'

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=50892


--
Edit this bug report at https://bugs.php.net/bug.php?id=50892&edit=1


Thread (21 messages)

« previous php.bugs (#207505) next »