Home >Backend Development >PHP Tutorial >php5中public,private,protected
This article will introduce to you the differences between public, private and protected in php5. All three of them are used in classes, but their attributes are completely different.
public: Public properties or methods
can be called in subclasses through self::var or self::method. Methods in parent classes can be called through parent::method, but public properties cannot be called.
In the instance, it can be called through $obj->var or self::method
protected: The protected type
can be called in the subclass through self::var or self::method, and it can be called through parent::method To call the method in the parent class
In the instance, you cannot call the protected type method or attribute through $obj->var
private: private type
The attribute or method of this type can only be used in this class, in Private type attributes and methods cannot be called in instances of this class, subclasses, or instances of subclasses
2. The difference between self and parent
a). These two objects are commonly used in subclasses. Their main difference is that self can call public or protected properties in the parent class, but parent cannot call
b).self:: It represents the static members (methods and properties) of the current class. Unlike $this, $ this refers to the current object
The above has introduced public, private, and protected in php5, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.