Home > Backend Development > PHP Tutorial > php5中public,private,protected

php5中public,private,protected

WBOY
Release: 2016-07-28 08:26:27
Original
1285 people have browsed it

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template