Home > Backend Development > PHP Tutorial > Things to note when php abstract classes inherit abstract classes, object-oriented learning

Things to note when php abstract classes inherit abstract classes, object-oriented learning

PHP中文网
Release: 2023-03-05 16:52:01
Original
1445 people have browsed it

When an abstract class inherits another abstract class, the abstract method of the abstract parent class cannot be overridden in the abstract class. Such usage can be understood as an extension of abstract classes.

The following example demonstrates that when an abstract class inherits from another abstract class, there is no need to override the abstract method.

<?
abstract class User 
{
    protected $sal = 0;  
 
    abstract function getSal();
    abstract function setSal($sal);  
}
abstract class VipUser extends User {
    
}
?>
Copy after login

After an abstract class is inherited, its abstract methods cannot be overridden. If overwriting occurs, the system will report an error.

<?
abstract class User 
{
    protected  $sal = 0;  
 
    abstract function getSal();
    abstract function setSal($sal);
}
abstract class VipUser extends User 
{
    abstract function setSal();
}
?>
Copy after login

Program running result:

Fatal error: Can&#39;t inherit abstract function User::setSal() (previously declared abstract in VipUser) in E:\PHPProjects\test.php on line 14
Copy after login


Conclusion: Abstract class inherits abstract class for the purpose of extending abstract class.

<?
abstract class User 
{
    protected $sal = 0;  
 
    abstract function getSal();
    abstract function setSal($sal);
}
abstract class VipUser extends User 
{
    protected $commision = 0;
    static abstract function getCommision();
    abstract function setCommision();   
}
?>
Copy after login

The above code extends the method of the parent class


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