SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Property Visibility Quest.

    Quick question on this statement from php.net:

    "Members declared protected can be accessed only within the class itself and by inherited and parent classes"

    So a parent class can access the protected properties of its children classes? Even if it doesn't declare the property itself?

    If so a quick use case of this?


    Thanks

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Yes that's correct, any class that gets extended by the parent automatically inherits properties both ways there for protected properties are accessible.

    PHP Code:
    class {
        protected 
    $text 'Hello World!';
    }

    class 
    extends class {
        public function 
    __construct() {
            echo 
    parent::$text;
        }
    }

    $myClass = new B


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
  •