Detailed explanation of the difference between private, public, and protected in PHP

Let’s briefly describe it first:
public means global, which can be accessed by both internal and external subclasses of the class;
private means private, which can only be used within this class;
protected means protected, only this class or subclass or parent class can access it ;

To explain more verbosely:

one,

public: public type

In subclasses,   public methods or attributes can be called through self::attribute name (or method name), and parent::method name is used to call parent class methods

In an instance, it is possible to call a method or property of the public type through $obj->property name (or method name)

protected: protected type

In the subclass, the protected method or attribute can be called by self::attribute name (or method name), and the parent class method can be called by parent::attribute name (or method name).

 The method or property of protected type cannot be called through $obj->property name (or method name) in an instance    

private: private type
properties or methods of this type can only be used in this class,

Properties and methods of private types cannot be called in instances of this class, in subclasses, or in instances of subclasses

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

  b).self:: It represents the static members (methods and properties) of the current class Unlike $this, $this refers to the current object

To be continued...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325128698&siteId=291194637