PHP reload

巴扎黑
Release: 2016-11-11 15:45:08
Original
1330 people have browsed it



PHP Overloading

A method in a class has the same name as another method, but the parameters are different. 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 method in the strict sense in PHP. Overload.

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, because methods inherited from the parent class may not be able to access properties or methods defined by the subclass, sometimes overloading is necessary.

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();
?>
运行该例子,输出:
我的名子叫:张三
我的年龄是:18
我正在人民大学学习
提示
如果父类定义方法时使用了 final 关键字,则不允许被子类方法覆盖。
访问父类被覆盖的方法
可以通过 :: 符号来访问父类被覆盖的方法或成员属性:
function say() {
    parent::say();
    //或者
    Person::say();
    echo "我在".$this->school."上学<br />";
Copy after login



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!