In PHP, the subclass inherits the parent class, but it also needs to extend the properties and methods of the parent class so that the subclasses can override the properties and methods, overwriting properties and methods with the same name as the parent class, but if the parent class's methods are more content, such as hundreds and thousands of lines of code, then simply use the "
Parent Class class Name:: MethodOr
Parent:: Method"To invoke the overridden method in the parent class, there is the
re-loading the parent class, and then add the statement that needs to be extended.
Override of Method
<?php class person{public $name; Public Function construct ($name = "") { $this->name= $name; } Public function Say () { echo "my Name". $this->name ; }}? ><?php class Student extends person{public $name; Public Function construct ($name = "") { $this->name = $name; } This defines a method with the same name in the parent class, overriding the speaking method in the parent class and overriding the public function say () { echo "I call". $this->name. "25 years old this year"; }}? >
overriding methods and access rights
When subclasses override the methods of the parent class, be aware that the methods that are overridden in subclasses must not have access to the methods that the parent class overrides. For example, the access permission for a method in a parent class is protected, then the permissions of the method overridden in the subclass are protected or public. If the method of the parent class is a public permission, the method to be overridden in the subclass can be public only. In summary, when overriding a method of a parent class in a subclass, be sure to override the permissions of the method overridden by the parent class.
Number of parameters When overriding
A subclass can have a different number of arguments than the parent class, as in the following construction method, a parameter $age is added.
<?phpclass Student extends person{public $name; public $age; Public Function construct ($name = "", $age =25) { $this->name = $name; $this->age = $age; } Public function Say () { echo "I called". $this->name. ", this year". $this->age. " years old "; } }?>
The above example, by overwriting us, implements the "method" extension.
But, like this solves the problem that we said above, but in the actual development, a method can not be a code or a few code, such as "person" class inside the "Say ()" Method has 100 code inside, If we want to cover this method to preserve the original function plus a little bit of functionality, it is necessary to rewrite the original 100 code once, plus the extension of a few code, this is good, and in some cases, the method in the parent class is not see the original code, this time how do you rewrite the original code? We also have a solution, that is, in the subclass of this method can be called to the parent class is overridden by the method, that is, the overridden method of the original function to take over and add their own point of function, you can use two methods to implement the method in the subclass to call the parent class is overridden:
One is to use the parent class's "Class Name::" To invoke the overridden method in the parent class;
One is to invoke the overridden method in the parent class using the "Parent::" Side test;
Extension of the method
<?phpclass Student extends person{public $name; public $age; Public Function construct ($name = "", $age =25) { parent::construct ($name, $age); $this->age = $age; } Public function Say () { parent::say (); echo ", this year". $this->age. " years old "; } }?>
As described above, only the properties and methods of the parent class are Reloaded , not the actual overloads, only the subclass has extended the parent class, and in PHP there is also the term overloaded (overloading), but it differs from the overloaded meaning in general language-oriented.