PHP5 public, private, protected three different kinds of attributes

Source: Internet
Author: User

Beginners PHP, see other people's packaging class has these three attributes, so look up the relevant data summarized the difference between the following:

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


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

Examples are as follows:

1<?PHP2     classBaseClass {3          Public $public= ' Public ';4         Private $private= ' Private ';5         protected $protected= ' protected ';6         function__construct () {}7         functionPrint_var () {8             Print $this- Public;Echo' <br/> ';9             Print $this-Private;Echo' <br/> ';Ten             Print $this-protected;Echo' <br/> '; One         } A     } -  -     classSubclassextendsBaseClass { the         //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 the definition of her class BaseClass can be used +         } A     } at      $obj 1=NewBaseClass (); -     $obj 1-Print_var (); -     //Echo $obj 1->protected;//error because it is protected, it can be called only in the inner or subclass parent class of this class . - //echo $obj 1->private;//error the same as private, only within this class call -     Echo $obj 1- Public; -     Echo"; in     $obj 2=Newsubclass (); -     Echo' <br/> '; to     Echo $obj 2- Public;Echo' <br/> '; +     Echo $obj 2-protected; -     //Echo $obj 2->private;//error the //echo $obj 2->protected;//error *?>

Overall

Public indicates the global, the inside and outside of the class can access;

Private means that only this class can be used internally;

Proteced is protected and is accessible only in this class or subclass or in the parent class;

PHP5 public, private, protected three different kinds of attributes

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.