SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)

    Why is my private member visible?

    Excuse the double-entendre-filled subject for this post

    I read an interesting tidbit on the PHP site:

    "Objects of the same type will have access to each others private and protected members even though they are not the same instances. This is because the implementation specific details are already known when inside those objects."

    PHP Code:
    <?php

    class Foo {
        private 
    $bar;
        
        public function 
    __construct($value) {
            
    $this->bar $value;
        }
        
        public function 
    getOtherBar($otherObject) {
            return 
    $otherObject->bar;
        }
        
    }

    $a = new Foo('baz');
    $b = new Foo('ban');

    echo 
    $a->getOtherBar($b);

    ?>
    Am I wrong in thinking that the above code should (in theory/ a perfect world) cause an error? Or am I missing something fundemental with relation to OO?

  2. #2
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Private members are accessible from within any instance of the same type


Bookmarks

Posting Permissions

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