Home > Backend Development > PHP Tutorial > PHP reloading_PHP tutorial

PHP reloading_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:11:04
Original
815 people have browsed it

PHP reload

PHP reload

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();
?>
Copy after login

Run this example, output:

我的名子叫:张三
我的年龄是:18
我正在人民大学学习
Copy after login

Tips

If the parent class uses the final keyword when defining a method, it is not allowed to be overridden by the subclass method.

Accessing the overridden methods of the parent class

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 />";
}
Copy after login

Scope parsing operator :: For usage, please refer to "PHP Range Parsing Operator ::".

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/931648.htmlTechArticlePHP Overloading PHP Overloading a method in a class has the same name as another method, but the parameters are different. This method Call it an overloaded method. It's a pity that PHP is a weakly typed language, so in...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template