Home >Backend Development >PHP Tutorial >PHP object-oriented - introduction to access modifiers_PHP tutorial

PHP object-oriented - introduction to access modifiers_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:14:20856browse

There are three access modifiers in PHP, namely:

public (public, default)
protected (protected)
private (private)

They can be used separately Used on the attributes and methods of a class (the attributes and methods of a class are collectively called members of a class) to modify the access rights of class members.
public (public, default)
In PHP5, if a class does not specify an access modifier for a member, the default is public access.

/*
The following two methods have the same effect of declaring access permissions
*/
function say(){};
public function say(){};

When a member of a class is declared with a public access modifier, the member can be accessed and operated by external code.
private (private)
Members defined as private are visible to all members within the class and have no access restrictions. Access is not allowed outside the class.
protected (protected)
protected is slightly more complicated. It is declared as a protected member and only allows access by subclasses of this class.

Access permission status table:

访问权限

public

protected

private

所有

子类

类内

Access rights

public

private

All

Subclass

Within class

http://www.bkjia.com/PHPjc/326321.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326321.html
TechArticle
There are three access modifiers in PHP, namely: public (public, default) protected (protected) private (private) They can be used on the attributes and methods of the class (class...
Statement:
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