What is an object?
Objects are data types that store data and information about how to work with the data. is an entity used to describe objective things in the system, which is a basic unit of the system. An object consists of a set of properties and a set of services that operate on that set of properties.
Grammar
In PHP, you must explicitly declare an object.
First we must declare the class of the object. We use the keyword class to declare a class, followed by the name of the class, the body is enclosed in the {} symbol, like this
Class class_name{... }
The class contains properties and methods.
Property
Declaring a variable by using the keyword var in the class definition creates a property of the class, also called the member property of the class.
Grammar:
Class class_name{ var $var _name;}
For example, if you define a person's class, then the person's name, age, gender, and so on can see the nature of this class.
Method
By declaring a function in the class definition, you create a method of the class.
Grammar:
Class class_name{ function function_name (arg1,arg2,......) { function function code }}
Application of the class
A class that defines properties and methods is a complete class that can contain a complete processing logic within a class. Use the New keyword to instantiate an object to apply the logic inside the class. You can instantiate multiple objects at the same time.
Grammar:
Object = new Class_name ();
After instantiating an object, use the-operator to access the object's member properties and methods.
Grammar:
object->var_name;object->function_name;
If you want to access the properties or methods of a member within a defined class, you can use a pseudo-variable $this. $this used to represent the current object or the object itself.
Example:
<?phpheader ("Content-type:text/html;charset=utf-8"); class Person { //Human member attribute var $name; The name of the person var $age; Person's age //person member say () method function say () { echo "My name is:". $this->name. " <br/> "; echo "My URL is:". $this->age; }} The class definition ends//instantiates an object $P1 = new Person (),//assigns a value to the $p 1 object property $p1->name = "topic.alibabacloud.com"; $p 1->age = ' www.php.cn ';// Call the Say () method in the object $p1->say ();? >
To run the example, output:
Here is a simple example of our composite data type "Object", for more object knowledge, please visit our object topic, the next section, we explain the two special data types in PHP "Resources"