Home >Backend Development >PHP Tutorial >PHP reloading_PHP tutorial
A method in one class has the same name as another method but has different parameters. This method is called an overloaded method.
Unfortunately, because PHP is a weakly typed language, the parameters of the method itself can receive different types of data, and because the PHP method can receive an indefinite number of parameters, there is no strict sense in PHP. Method overloading.
Overloading in PHP means defining a method with the same name as the parent class in a subclass, and this method will override the method of the parent class in the subclass.
In subclasses, overloading is sometimes necessary because methods inherited from the parent class may not be able to access properties or methods defined by the subclass.
Example:
<?php class Person { var $name; var $age; function say() { echo "我的名字叫:".$this->name."<br />"; echo "我的年龄是:".$this->age; } } // 类的继承 class Student extends Person { var $school; //学生所在学校的属性 function say() { echo "我的名字叫:".$this->name."<br />"; echo "我的年龄是:".$this->age."<br />"; echo "我正在".$this->school."学习"; } } $t1 = new Student(); $t1->name = "张三"; $t1->age = "18"; $t1->school = "人民大学"; $t1->say(); ?>
Run this example, output:
我的名子叫:张三 我的年龄是:18 我正在人民大学学习
If the parent class uses the final keyword when defining a method, it is not allowed to be overridden by the subclass method.
You can access the overridden methods or member properties of the parent class through the :: symbol:
function say() { parent::say(); //或者 Person::say(); echo "我在".$this->school."上学<br />"; }
Scope parsing operator :: For usage, please refer to "PHP Range Parsing Operator ::".