Home  >  Article  >  Backend Development  >  What is the usage of public in php

What is the usage of public in php

WBOY
WBOYOriginal
2022-02-22 14:04:115824browse

In PHP, public is an access modifier, used to limit the accessibility of modified members. It means "public", that is, it can be accessed in all locations. The syntax is "class Class name {public property or method definition}".

What is the usage of public in php

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.

What is the usage of public in php

Access control modifiers

形式:
class  类名{
访问控制修饰符  属性或方法定义;
}

There are 3 access modifiers:

  • ##public Public: accessible (used) everywhere.

  • protected Protected: can only be accessed (used) within the class and in subclasses or parent classes of the class.

  • private: can only be accessed (used) within the class.

Their function is to "limit" the "accessibility" of the members they modify;

Accessibility:

It is the "validity" (legality) of using such two syntax forms in the code:

Object->Instance properties or methods;

Class:: Static properties or methods;

The access control modifier needs to be used in conjunction with the location of this syntax form to determine whether it is accessible.

There are 3 access locations (scopes):

Inside a certain class:

Inside a class that inherits a certain class:

A certain Outside the class:

Then, their combined accessibility is as follows:

What is the usage of public in php

Summary:

1, public modified Members can be accessed anywhere;

2, inside the class, members at any level can be accessed;

3, public has the broadest accessibility; private has the narrowest accessibility Sexual; protected is in the middle;

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of What is the usage of public in php. For more information, please follow other related articles on the PHP Chinese website!

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