-
- class Person
- {
- //The following are the member attributes of the person
- var $name; //The name of the person
- var $***; //The gender of the person
- var $age ; //The age of the person
- //The following is the member method of the person
- function say() //The method by which this person can speak
- {
- echo "This person is talking";
- }
- function run() //This person Methods that can walk
- {
- echo "This person is walking";
- }
- }
- ?>
-
Copy code
3.5. How to instantiate objects
As mentioned above, the unit of PHP object-oriented program is the object, but the object is instantiated through the class. Since our class will be declared, the next step is to instantiate the object. After defining the class, we use the new keyword to generate an object.
$Object name = new Class name();
Object->Properties $p1->name; $p2->age; $p3->***;
Object->Method $p1->say(); $p2->run();
5.7. The use of special reference "$this" Now we know how to access members in an object, which is accessed through "Object -> Members", which is a form of accessing members in an object outside the object. So if I want to let the methods in the object access the properties of this object inside the object, or the methods in the object to call other methods of this object, what should we do? Because all members in the object must be called using the object, including calls between internal members of the object, PHP provides me with a reference to this object, $this, and each object has a reference to the object. $this represents this object and completes the call to the internal members of the object. The original meaning of this is "this". In the above example, we instantiate three instance objects $P1, $P2, and $P3. There is one $this each representing objects $p1, $p2, and $p3.
-
- class Person
- {
- //The following are the member attributes of the person
- var $name; //The name of the person
- var $***; //The gender of the person
- var $ age; //The person's age
- //The following is the person's member method
- function say() //The method this person can speak
- { echo "My name is: ".$this->name." Gender : ".$this->***." My age is: ".$this->age."
- ";
- }
Copy code
8.Construction method and destruction method
Most classes have a special method called a constructor. When an object is created, it will automatically call the constructor, that is, when the new keyword is used to instantiate the object, the constructor will be automatically called.
The declaration of a constructor is the same as the declaration of other operations, except that its name must be __construct(). This is a change in PHP5. In previous versions, the name of the constructor must be the same as the class name. This can still be used in PHP5, but few people use it now. The advantage of this is that the constructor can be Independent of the class name, there is no need to change the corresponding constructor name when the class name changes. For backward compatibility, if there is no method named __construct() in a class, PHP will search for a constructor method written in php4 with the same name as the class name.
Format: function __construct ([parameter]) { ... ... }
Only one constructor can be declared in a class, but the constructor will only be called once every time an object is created. This method cannot be called actively, so it is usually used to perform some useful initialization tasks. For example, the corresponding properties are assigned initial values when the object is created.
-
- //Create a human
- class Person
- {
- //The following are the member attributes of the person
- var $name; //The name of the person
- var $***; //Person Gender
- var $age; //Age of a person
- //Define a constructor parameter as name $name, gender $*** and age $age
- function __construct($name, $***, $age)
- {
- //The $name passed in through the construction method is assigned an initial value to the member attribute $this->name
- $this->name=$name;
- //The $*** passed in through the construction method is assigned to The member attribute $this->*** is assigned an initial value
- $this->***=$***;
- //The $age passed in through the construction method is assigned to the member attribute $this->age Initial value
- $this->age=$age;
- }
- //How this person speaks
- function say()
- {
- echo "My name is: ".$this->name." Gender: ".$this->***." My age is: ".$this->age."
- ";
- }
- }
- //Create 3 objects $p1, p2 through the construction method , $p3, respectively pass in three different actual parameters: name, gender and age
- $p1=new Person("Zhang San", "Male", 20);
- $p2=new Person("Li Si", "Female", 30);
- $p3=new Person("王五","Male", 40);
- //The following accesses the speaking method in the $p1 object
- $p1->say();
- / /The following accesses the speaking method in the $p2 object
- $p2->say();
- //The following accesses the speaking method in the $p3 object
- $p3->say();
- ?>
-
Copy the code
The output result is:
My name is: Zhang San Gender: Male My age is: 20
My name is: Li Si Gender: Female My age is: 30
My name is: Wang Wu Gender: Male My age is: 40
The opposite of the constructor is the destructor. The destructor is a newly added content of PHP5. There is no destructor in PHP4. The destructor allows you to perform some operations or complete some functions before destroying a class, such as closing files, releasing result sets, etc. The destructor will be deleted when all references to an object are deleted or when the object is explicitly destroyed. When executed, that is, the destructor is called before the object is destroyed in memory. Similar to the name of the constructor, the name of a class's destructor must be __destruct(). The destructor cannot take any parameters.
Format: function __destruct ( ) { ... ... }
-
-
- //Create a human
- class Person
- {
- //The following are the member attributes of the person
- var $name; //The person’s name
- var $** *; //Person’s gender
- var $age; //Person’s age
- //Define a constructor method parameter as name $name, gender $*** and age $age
Function __construct ($name, $***, $age)
- {
- //The $name passed in through the construction method assigns an initial value to the member attribute $this->name
- $this->name=$name;
- //The $*** passed in through the construction method is assigned an initial value to the member attribute $this->***
- $this->***=$***;
- //It is passed in through the construction method $age assigns an initial value to the member attribute $this->age
- $this->age=$age;
- }
- //How this person speaks
- function say()
- {
- echo "My name My name is: ".$this->name." Gender: ".$this->***." My age is: ".$this->age."
- ";
- }
- // This is a destructor, which is called before the object is destroyed.
-
If you provide an interface outside the class, you can provide setting methods and get methods for private attributes outside the class to operate the private attributes. For example: prvate $age; //Private attribute age
-
- function setAge($age) //Provide a public method for setting age externally
- {
- if($age<0 || $age>130) //When assigning values to attributes, in order to avoid Illegal values are set to attributes
- return;
- $this->age=$age;
- }
- function getAge() //Provide a public method to get age externally
- {
- return($this->age);
- }
Copy the code
The above method is to set and get the value for a member attribute. Of course, you can also use the same method to assign and get the value for each attribute to complete the operation outside the class. Access work.
JAVABEAN is the same! ! !
10. Application of the four methods __set() __get() __isset() __unset()
Generally speaking, class attributes are always defined as private, which is more in line with realistic logic. However, reading and assigning operations to attributes are very frequent, so in PHP5, two functions "__get()" and "__set()" are predefined to obtain and assign their attributes, and "__isset" to check the attributes. ()" and the method to delete attributes "__unset()".
In the previous section, we set and obtained methods for each attribute. PHP5 provides us with special methods for setting and obtaining values for attributes, "__set()" and "__get()". Methods, these two methods do not exist by default, but are manually added to the class. Like the constructor method (__construct()), they will only exist if they are added to the class. You can add these two methods in the following way. Methods, of course, can also be added according to personal style:
-
-
//__get() method is used to get private properties
- private function __get($property_name)
- {
if(isset($this-> ;$property_name))
- {
- return($this->$property_name);
- }else
- {
- return(NULL);
- }
- }
- //__set() method is used to set private properties
- private function __set ($property_name, $value)
- {
- $this->$property_name = $value;
- }
-
-
Copy code
__get() method: This method is used to get the private member attribute value. It has one parameter. The parameter is passed in the name of the member attribute you want to get, and the obtained attribute value is returned. This method does not need to be called manually, because we This method can also be made into a private method, which is automatically called by the object when the private property is directly obtained. Because the private properties have been encapsulated, the value cannot be obtained directly (for example: "echo $p1->name" is wrong to obtain directly), but if you add this method to the class, use "echo $p1->name". $p1->name" When such a statement directly obtains the value, it will automatically call the __get($property_name) method, passing the property name to the parameter $property_name, and through the internal execution of this method, the private property we passed in will be returned. value. If the member properties are not encapsulated as private, the object itself will not automatically call this method.
__set() method: This method is used to set values for private member attributes. It has two parameters. The first parameter is the name of the attribute you want to set the value for. The second parameter is the value you want to set for the attribute. There is no return. value. This method also does not need to be called manually. It can also be made private. It is automatically called when directly setting the private attribute value. The same private attribute has been encapsulated. If there is no __set() method, it is Not allowed, for example: $this->name='zhangsan', this will cause an error, but if you add the __set($property_name, $value) method to the class, when directly assigning values to private properties, It will be called automatically, passing attributes such as name to $property_name, and passing the value "zhangsan" to be assigned to $value. Through the execution of this method, the purpose of assignment is achieved. If the member properties are not encapsulated as private, the object itself will not automatically call this method. In order not to pass in illegal values, you can also make a judgment in this method.
-
- class Person
- {
- //The following are the member attributes of the person, which are all encapsulated private members
- private $name; //The name of the person
- private $***; / /Person’s gender
- private $age; //Person’s age
- //__get() method is used to obtain private properties
- private function __get($property_name)
- {
- echo "Automatically called when directly obtaining private property values Got this __get() method
- ";
- if(isset($this->$property_name))
- {
- return($this->$property_name);
- }
- else
- {
- return(NULL);
- }
- }
- //__set() method is used to set private properties
- private function __set($property_name, $value)
- {
- echo "When directly setting the private property value, this __set() method is automatically called Assign a value to a private property
- ";
- $this->$property_name = $value;
- }
- }
- $p1=new Person();
- //The operation of directly assigning a value to a private property will automatically call __set() Method to assign value
- $p1->name="Zhang San";
- $p1->***="Male";
- $p1->age=20;
- //Get the value of the private attribute directly, The __get() method will be automatically called to return the value of the member attribute
- echo "Name:".$p1->name."
- ";
- echo "Gender:".$p1->***."
- ";
- echo "Age:".$p1->age."
- ";
- ?>
Copy code
|