PHP Object-oriented private, protected, public three of the difference between the detailed

Source: Internet
Author: User
This article is mainly to share with you PHP object-oriented private, protected, public, the difference between the three, I hope to help everyone.

        Private     protected public       This class within     y           y y              sub-class     n           y y              outer       n           n              y

Attention:
In Java, if no arguments are written before the property/method
That Public/protected/private is not written, is also possible, friendly

Class Human {Private $name = ' Zhangsan ';    protected $money = 3000;    Public $age = 28;        Public function say () {echo ' My name ', $this->name, ' <br/> ';        Echo ' I have ', $this->money, ' Yuan Qian <br/> ';    Echo ' I this year ', $this->age, ' old ';    }}class Stu extends Human {private $friend = ' floret ';        Public function Talk () {echo ' My name ', $this->name, ' <br/> ';        Echo ' I have ', $this->money, ' Yuan Qian <br/> ';            Echo ' I this year ', $this->age, ' old <br/> '; }} $ming = new Stu (); Echo $ming->age, ' <br/> '; 28echo $ming->friend; Error: Privateecho cannot be called outside the class $ming->money; Error: Protected property cannot be called Outside the class $ming->talk ();/** error: notice:undefined property:stu:: $name in ... I have 3000 dollars, I'm 28 years old. Analysis Reason: Undefined property:stu:: $name This is the description: Stu object does not have a name property but said yesterday, is not the private can inherit it? Yes, but the system has tags. Marks it as a private property at the parent level. Therefore, it is not authorized to call, which causes this error to occur. Can be analyzed: protected can be accessed within the subclass protected can be accessed in subclasses, can be accessed within this class? A: Of course.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.