Definition, usage, and differentiation of public,private,protected attributes in PHP

Source: Internet
Author: User
Public: Common property or method

Can be called by Self::var or Self::method in a subclass, you can invoke a method in the parent class by Parent::method, but you cannot call the public property.

You can invoke the $obj->var or Self::method in the instance.

Protected: Protected Type

Can be called through Self::var or Self::method in a subclass, you can call methods in the parent class by Parent::method
Methods or properties of the protected type cannot be called through $obj->var in an instance

Private: Proprietary type

A property or method of that type can only be used in that class, and the properties and methods of private types cannot be called in instances, subclasses, or instances of subclasses of that class

The difference between 2.self and parent
A). These two objects are commonly used in subclasses. The main difference is that self can invoke a public or protected property in the parent class, but parent cannot call

b). Self:: It indicates that static members (methods and properties) of the current class are different from $this, $this refers to the current object

<?phpclass BaseClass {Public $public = ' public ';  Private $private = ' private ';  Protected $protected = ' protected ';    function construct () {} function Print_var () {print $this->public;echo ' <br/> '; Print $this->private;    echo ' <br/> '; Print $this->protected;  echo ' <br/> ';  }}class Subclass extends BaseClass {//Public $public = ' public2 ';  protected $protected = ' protected2 ';    function construct () {echo $this->protected;//can be accessed because the class is defined as protected, so in this class or subclass, you can also repeat the value in the subclass Echo ' <br/> ';  Echo $this->private;//error because it is private only in defining her class BaseClass can be used}} $obj 1 = new BaseClass ();  $obj 1->print_var (); Echo $obj 1->protected;//error because it is protected, only in this class inside or in the subclass parent class can call//echo $obj 1->private;//error as private, only within this class call Echo $  obj1->public;  echo "

Summarize

Public represents the global, and the inner and outer subclasses of the class can be accessed;
Private means that only this class can be used internally;
Protected is protected and is accessible only in this class or subclass or in the parent class;

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.