#26325 [Opn]: At least a notice when accessing private members in a derived class?

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default #26325 [Opn]: At least a notice when accessing private members in a derived class?

    ID: 26325
    User updated by: drm at melp dot nl
    Reported By: drm at melp dot nl
    Status: Open
    Bug Type: Feature/Change Request
    Operating System: Windows XP
    PHP Version: 5.0.0b2 (beta2)
    New Comment:

    Here's a more explanatory piece of code:
    [url]http://gerard.yoursite.nl/php.net/private-members.phps[/url]


    Previous Comments:
    ------------------------------------------------------------------------

    [2003-11-19 17:49:24] drm at melp dot nl

    Description:
    ------------
    Hi :)

    I read in the new features list of Zend Engine 2 that access modifiers
    like private/protected are introduced, but something really weird comes
    in mind when reading carefully.

    Private members are returned _empty_ wheing tried to be accessed from
    derived classes. "Intended behaviour", is said. OK, i can live with
    that. But even when i turned on notices with error_reporting, nothing
    is noticed?

    The case grows when looking at the following code sample. This could
    produce very weird bugs when writing PHP code (i hope you can see that
    ;)), so I would like to convince you all to at least implement a Notice
    when trying to access (non-defined) private (parent) members?

    It would do the coders not using E_ALL no harm :) Please consider this
    :)

    Reproduce code:
    ---------------
    error_reporting ( E_ALL );
    class Test {
    private $member;
    function __construct () { $this->member = "Test constructor";
    }
    function __toString () { return "Member contains:
    {$this->member}"; }
    function getMember () { return $this->member; }
    function setMember ( $m ) { $this->member = $m; }
    }
    class DeriveTest extends Test {
    function __construct () { parent::__construct (); }
    function __toString () { return "Member contains:
    {$this->member}, though getMember() says: " . $this->getMember() ."?";
    }
    function setMember ( $m ) { $this->member = $m; }
    }
    $o = new DeriveTest ();
    echo $o, '<br>';
    $o->setMember ( "a" );
    echo $o, '<br>';


    Expected result:
    ----------------
    The expected result would be for me:

    Notice: undefined member ``DeriveTest::$member'' in ...\test.php on
    line 12
    Member contains: , though getMember() says: Test constructor?
    Member contains: a, though getMember() says: Test constructor?

    or something of the sort

    Actual result:
    --------------
    The actual result is:

    Member contains: , though getMember() says: Test constructor?
    Member contains: a, though getMember() says: Test constructor?


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


    --
    Edit this bug report at [url]http://bugs.php.net/?id=26325&edit=1[/url]
    drm at melp dot nl Guest

  2. Similar Questions and Discussions

    1. Problem setting and accessing class members
      Hello, I have the following piece of php code: <code snippet> class Section { var $id; var $name; var $color;
    2. #26325 [Bgs->Opn]: At least a notice when accessing private members in a derived class?
      ID: 26325 User updated by: drm at melp dot nl Reported By: drm at melp dot nl -Status: Bogus +Status: ...
    3. #26325 [Opn->Bgs]: At least a notice when accessing private members in a derived class?
      ID: 26325 Updated by: jay@php.net Reported By: drm at melp dot nl -Status: Open +Status: Bogus Bug...
    4. #26325 [NEW]: At least a notice when accessing private members in a derived class?
      From: drm at melp dot nl Operating system: Windows XP PHP version: 5.0.0b2 (beta2) PHP Bug Type: Feature/Change Request Bug...
    5. Access DataGrid declared in derived class from base class??
      Hi! I have a base class called ListBase.vb, from which I derive EmplList.ascx.vb. In EmplList.ascx.vb I declared a...
  3. #2

    Default #26325 [Opn]: At least a notice when accessing private members in a derived class?

    ID: 26325
    Updated by: [email]abies@php.net[/email]
    Reported By: drm at melp dot nl
    Status: Open
    Bug Type: Feature/Change Request
    Operating System: Windows XP
    PHP Version: 5.0.0b2 (beta2)
    New Comment:

    Actually, the main issue here is that PHP doesn't give a notice at all
    if you access an undefined property, whether it is a private property
    in a base class or not.




    Previous Comments:
    ------------------------------------------------------------------------

    [2003-11-22 09:05:03] drm at melp dot nl

    Excuse me, but you are *totally* missing my point here. You are telling
    me things i had already pointed out in the first post.

    I'll reduce my feature request to one simple line:

    Can *at least* a "notice" be triggered when a private member variable
    does not exist but is accessed? And by accessed i do NOT mean
    assigned!

    You're saying it behaves the same way php4 does, but that's just plain
    bs. See the following code in PHP4:

    <?
    error_reporting ( E_ALL );
    class Test {
    function getMember () {
    return $this->member;
    }
    }
    $t = new Test ();
    echo $t->getMember ();
    ?>

    This would yield the following notice:
    Notice: Undefined property: member in test.php on line 5

    All i'm asking is that some notice of the SAME sort can be implemented
    in php5 when trying to access parent private members.

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

    [2003-11-20 11:46:55] [email]jay@php.net[/email]

    There are two issues here.

    First, PHP 5 is just acting like PHP 4 did -- you can
    initialize a member of an object without explicitly
    declaring it in the class definition and PHP won't
    complain.

    Second, Test::$member and DeriveTest::$member aren't the
    same things. Test::$member being private, it's only
    accessible and visible from within Test. DeriveTest has no
    idea it exists, so it creates its own public member called
    $member. If you do a print_r($o) at the end of your
    script, you'll see that there are two members called
    $member, one of which is private and the other public.

    So yes, this is intended behaviour. One of the main ideas
    of private members is that they aren't even visible from
    derived classes. If you try to access a private member
    from a parent class, the derived class won't see it and
    will instead try using its own member, and if that doesn't
    exist it will implicitly create it.

    J

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

    [2003-11-20 04:45:48] drm at melp dot nl

    Here's a more explanatory piece of code:
    [url]http://gerard.yoursite.nl/php.net/private-members.phps[/url]

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

    [2003-11-19 17:49:24] drm at melp dot nl

    Description:
    ------------
    Hi :)

    I read in the new features list of Zend Engine 2 that access modifiers
    like private/protected are introduced, but something really weird comes
    in mind when reading carefully.

    Private members are returned _empty_ wheing tried to be accessed from
    derived classes. "Intended behaviour", is said. OK, i can live with
    that. But even when i turned on notices with error_reporting, nothing
    is noticed?

    The case grows when looking at the following code sample. This could
    produce very weird bugs when writing PHP code (i hope you can see that
    ;)), so I would like to convince you all to at least implement a Notice
    when trying to access (non-defined) private (parent) members?

    It would do the coders not using E_ALL no harm :) Please consider this
    :)

    Reproduce code:
    ---------------
    error_reporting ( E_ALL );
    class Test {
    private $member;
    function __construct () { $this->member = "Test constructor";
    }
    function __toString () { return "Member contains:
    {$this->member}"; }
    function getMember () { return $this->member; }
    function setMember ( $m ) { $this->member = $m; }
    }
    class DeriveTest extends Test {
    function __construct () { parent::__construct (); }
    function __toString () { return "Member contains:
    {$this->member}, though getMember() says: " . $this->getMember() ."?";
    }
    function setMember ( $m ) { $this->member = $m; }
    }
    $o = new DeriveTest ();
    echo $o, '<br>';
    $o->setMember ( "a" );
    echo $o, '<br>';


    Expected result:
    ----------------
    The expected result would be for me:

    Notice: undefined member ``DeriveTest::$member'' in ...\test.php on
    line 12
    Member contains: , though getMember() says: Test constructor?
    Member contains: a, though getMember() says: Test constructor?

    or something of the sort

    Actual result:
    --------------
    The actual result is:

    Member contains: , though getMember() says: Test constructor?
    Member contains: a, though getMember() says: Test constructor?


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


    --
    Edit this bug report at [url]http://bugs.php.net/?id=26325&edit=1[/url]
    abies@php.net Guest

  4. #3

    Default #26325 [Opn]: At least a notice when accessing private members in a derived class?

    ID: 26325
    User updated by: drm at melp dot nl
    Reported By: drm at melp dot nl
    Status: Open
    Bug Type: Feature/Change Request
    Operating System: Windows XP
    PHP Version: 5.0.0b2 (beta2)
    New Comment:

    You are right, i hadn't researched the problem that well, since i
    assumed php5 would treat properties the php4-style the same way as php4
    itself. But you are right; the code sample above when ran in php5
    doesn't give a notice, though php4 does.


    Previous Comments:
    ------------------------------------------------------------------------

    [2003-11-22 10:26:03] [email]abies@php.net[/email]

    Actually, the main issue here is that PHP doesn't give a notice at all
    if you access an undefined property, whether it is a private property
    in a base class or not.



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

    [2003-11-22 09:05:03] drm at melp dot nl

    Excuse me, but you are *totally* missing my point here. You are telling
    me things i had already pointed out in the first post.

    I'll reduce my feature request to one simple line:

    Can *at least* a "notice" be triggered when a private member variable
    does not exist but is accessed? And by accessed i do NOT mean
    assigned!

    You're saying it behaves the same way php4 does, but that's just plain
    bs. See the following code in PHP4:

    <?
    error_reporting ( E_ALL );
    class Test {
    function getMember () {
    return $this->member;
    }
    }
    $t = new Test ();
    echo $t->getMember ();
    ?>

    This would yield the following notice:
    Notice: Undefined property: member in test.php on line 5

    All i'm asking is that some notice of the SAME sort can be implemented
    in php5 when trying to access parent private members.

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

    [2003-11-20 11:46:55] [email]jay@php.net[/email]

    There are two issues here.

    First, PHP 5 is just acting like PHP 4 did -- you can
    initialize a member of an object without explicitly
    declaring it in the class definition and PHP won't
    complain.

    Second, Test::$member and DeriveTest::$member aren't the
    same things. Test::$member being private, it's only
    accessible and visible from within Test. DeriveTest has no
    idea it exists, so it creates its own public member called
    $member. If you do a print_r($o) at the end of your
    script, you'll see that there are two members called
    $member, one of which is private and the other public.

    So yes, this is intended behaviour. One of the main ideas
    of private members is that they aren't even visible from
    derived classes. If you try to access a private member
    from a parent class, the derived class won't see it and
    will instead try using its own member, and if that doesn't
    exist it will implicitly create it.

    J

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

    [2003-11-20 04:45:48] drm at melp dot nl

    Here's a more explanatory piece of code:
    [url]http://gerard.yoursite.nl/php.net/private-members.phps[/url]

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

    [2003-11-19 17:49:24] drm at melp dot nl

    Description:
    ------------
    Hi :)

    I read in the new features list of Zend Engine 2 that access modifiers
    like private/protected are introduced, but something really weird comes
    in mind when reading carefully.

    Private members are returned _empty_ wheing tried to be accessed from
    derived classes. "Intended behaviour", is said. OK, i can live with
    that. But even when i turned on notices with error_reporting, nothing
    is noticed?

    The case grows when looking at the following code sample. This could
    produce very weird bugs when writing PHP code (i hope you can see that
    ;)), so I would like to convince you all to at least implement a Notice
    when trying to access (non-defined) private (parent) members?

    It would do the coders not using E_ALL no harm :) Please consider this
    :)

    Reproduce code:
    ---------------
    error_reporting ( E_ALL );
    class Test {
    private $member;
    function __construct () { $this->member = "Test constructor";
    }
    function __toString () { return "Member contains:
    {$this->member}"; }
    function getMember () { return $this->member; }
    function setMember ( $m ) { $this->member = $m; }
    }
    class DeriveTest extends Test {
    function __construct () { parent::__construct (); }
    function __toString () { return "Member contains:
    {$this->member}, though getMember() says: " . $this->getMember() ."?";
    }
    function setMember ( $m ) { $this->member = $m; }
    }
    $o = new DeriveTest ();
    echo $o, '<br>';
    $o->setMember ( "a" );
    echo $o, '<br>';


    Expected result:
    ----------------
    The expected result would be for me:

    Notice: undefined member ``DeriveTest::$member'' in ...\test.php on
    line 12
    Member contains: , though getMember() says: Test constructor?
    Member contains: a, though getMember() says: Test constructor?

    or something of the sort

    Actual result:
    --------------
    The actual result is:

    Member contains: , though getMember() says: Test constructor?
    Member contains: a, though getMember() says: Test constructor?


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


    --
    Edit this bug report at [url]http://bugs.php.net/?id=26325&edit=1[/url]
    drm at melp dot nl Guest

  5. #4

    Default #26325 [Opn]: At least a notice when accessing private members in a derived class?

    ID: 26325
    Updated by: [email]jay@php.net[/email]
    Reported By: drm at melp dot nl
    Status: Open
    -Bug Type: Feature/Change Request
    +Bug Type: Zend Engine 2 problem
    Operating System: Windows XP
    PHP Version: 5.0.0b2 (beta2)
    New Comment:

    Take your original example and edit it so it works in PHP
    4. It runs in both 4 and 5 without a notice. Based on your
    original example code, that isn't "just plain bs."

    Upon further inspection based on your second example,
    there is definitely something weird going on here. This is
    most likely related to #26182.

    J

    I'm not positive, but I think this may have something to
    do with #26182.

    J


    Previous Comments:
    ------------------------------------------------------------------------

    [2003-11-22 18:57:46] drm at melp dot nl

    You are right, i hadn't researched the problem that well, since i
    assumed php5 would treat properties the php4-style the same way as php4
    itself. But you are right; the code sample above when ran in php5
    doesn't give a notice, though php4 does.

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

    [2003-11-22 10:26:03] [email]abies@php.net[/email]

    Actually, the main issue here is that PHP doesn't give a notice at all
    if you access an undefined property, whether it is a private property
    in a base class or not.



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

    [2003-11-22 09:05:03] drm at melp dot nl

    Excuse me, but you are *totally* missing my point here. You are telling
    me things i had already pointed out in the first post.

    I'll reduce my feature request to one simple line:

    Can *at least* a "notice" be triggered when a private member variable
    does not exist but is accessed? And by accessed i do NOT mean
    assigned!

    You're saying it behaves the same way php4 does, but that's just plain
    bs. See the following code in PHP4:

    <?
    error_reporting ( E_ALL );
    class Test {
    function getMember () {
    return $this->member;
    }
    }
    $t = new Test ();
    echo $t->getMember ();
    ?>

    This would yield the following notice:
    Notice: Undefined property: member in test.php on line 5

    All i'm asking is that some notice of the SAME sort can be implemented
    in php5 when trying to access parent private members.

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

    [2003-11-20 11:46:55] [email]jay@php.net[/email]

    There are two issues here.

    First, PHP 5 is just acting like PHP 4 did -- you can
    initialize a member of an object without explicitly
    declaring it in the class definition and PHP won't
    complain.

    Second, Test::$member and DeriveTest::$member aren't the
    same things. Test::$member being private, it's only
    accessible and visible from within Test. DeriveTest has no
    idea it exists, so it creates its own public member called
    $member. If you do a print_r($o) at the end of your
    script, you'll see that there are two members called
    $member, one of which is private and the other public.

    So yes, this is intended behaviour. One of the main ideas
    of private members is that they aren't even visible from
    derived classes. If you try to access a private member
    from a parent class, the derived class won't see it and
    will instead try using its own member, and if that doesn't
    exist it will implicitly create it.

    J

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

    [2003-11-20 04:45:48] drm at melp dot nl

    Here's a more explanatory piece of code:
    [url]http://gerard.yoursite.nl/php.net/private-members.phps[/url]

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

    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
    [url]http://bugs.php.net/26325[/url]

    --
    Edit this bug report at [url]http://bugs.php.net/?id=26325&edit=1[/url]
    jay@php.net Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139