The variable members of the class are called attributes. The attribute declaration starts with the keywords Public, Protected, and Private, and is followed by an ordinary variable declaration. The variables in the attribute can
Initialization, but the initialized value must be a constant.
If you use var to declare attributes directly without using public, protected or private, PHP will treat it as public.
In the member method of the class, you can use -> (object operator) such as $this->property (property is the property name) to access non-static data.
Static properties use:: (double colon) self::$property to access
For example, call a member method of a class
<?php class SimpleClass{ public $name="Tome"; //若不声明public 则默认是public $name="Tome"; } //实例化对象访问属性用-> $simple=new SimpleClass(); $simple->name; ?>
PHP’s property or method Access control is achieved by adding the keywords public (public), protected (protected) or private (private) in front.
Public: Public class members can be accessed from anywhere.
protected (protected): Protected class members can be accessed by itself and its subclasses and parent classes.
private (private): Private class members can only be accessed by the class in which they are defined.
Class attributes must be defined as one of public, protected, and private. If defined with var, it is considered public.
The above is the detailed content of What attributes does the php class have?. For more information, please follow other related articles on the PHP Chinese website!