This article gives you a simple example of the PHP class and object of public properties and private property usage, there is a need to understand the friend can refer to.
Private properties
A property that defines private properties can only be used in the class, which is called through $this->. An external reference to the private property will cause an error.
Instance:
The code is as follows |
Copy Code |
Class people{ Private $name = "Li Ming"; } $p =new people (); Echo $p->name; ?> |
Note: A field that has a private property set cannot be used in a subclass.
Public properties
In the operation of a PHP class, declaring a field uses public, private, protected, final, const, static to describe the scope of the object's data element, which is known as qualifying access control characters.
Properties that have a keyword public declaration are called Common Properties and are free to read and modify inside and outside of the class. This is obviously not safe enough to destroy the characteristics of class encapsulation.
If the field is not declared, the default is public.
Instance:
The code is as follows |
Copy Code |
Class man{ Public $name = "John"; /* Set public properties */ var $age = 20; } $a =new man (); echo $a->name. " "; Echo $a->age; ?> |
Changing property values
If the property is declared public, you can change the value of the property or assign an undefined property value as needed when you call it externally.
Instance:
The code is as follows |
Copy Code |
Class man{ Public $name = "John"; var $age; } $a =new man (); $a->name= "Tom"; /* Change attribute values */ $a->age=20; /* Give attribute value */ echo $a->name. " "; Echo $a->age; ?> |
http://www.bkjia.com/PHPjc/628817.html www.bkjia.com true http://www.bkjia.com/PHPjc/628817.html techarticle This article gives you a simple example of the PHP class and object of public properties and private property usage, there is a need to understand the friend can refer to. Private properties define the properties of private properties ...