Home > Backend Development > PHP Problem > What is the method of parent in php

What is the method of parent in php

coldplay.xixi
Release: 2023-03-08 07:26:01
Original
2680 people have browsed it

The parent method in php can be used to call member methods defined in the parent class. The code is [class Manager extends employee {public function getSal(){parent::getSal()}].

What is the method of parent in php

The operating environment of this tutorial: Windows 7 system, PHP version 5.6, DELL G3 computer. This method is suitable for all brands of computers.

Parent method in php: to reference the method of the parent class.

  • parent:: Can be used to call member methods defined in the parent class.

  • parent:: traces back beyond the direct parent class.

Call the parent class method through parent::

<!-- 声明一个员工类,经理类继承自员工类 -->
<?php
class employee{
    protected  $sal=3000;        
    public function getSal(){
        $this->sal = $this->sal + 1200;        
        return $this->sal ;
    }    
}
class Manager extends employee {
    //如果想让经理在员工工资的基础上多发1500元.
    //必须先调用父类的getSal()方法.
    public function getSal(){        
        parent::getSal();// 这里调用了父类的方法.
        $this->sal = $this->sal + 1500;        
        return $this->sal ;
    }    
}
$emp = new employee();
echo "普通员工的工资是 " . $emp->getSal();
echo "<br>";
$manager = new Manager();
echo "经理的工资是: " . $manager->getSal();
Copy after login

Related video recommendations: PHP programming from entry to proficiency

The above is the detailed content of What is the method of parent in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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