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

What is the method of parent in php

coldplay.xixi
coldplay.xixiOriginal
2021-02-20 16:30:052393browse

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();

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!

Statement:
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