You can use abstract to modify a class or method.
A class modified with abstract indicates that this class is an abstract class, and a method modified with abstract indicates that this method is an abstract method.
Abstract classes cannot be instantiated.
Abstract methods only have method declarations but no implementation content of the method.
abstract abstract class
You can use abstract to modify a class.
A class modified with abstract indicates that this class is an abstract class.
Abstract classes cannot be instantiated.
This is a simple abstract method. If it is instantiated directly, the system will report an error.
<?php //定义一个抽象类 abstract class User { public function __toString() { return get_class($this); } } //实例化这个类会出现错误 echo new User(); ?>
<?php //定义一个抽象类 abstract class User { public function __toString() { return get_class($this); } } //实例化这个类会出现错误 echo new User(); class NormalUser extends User { } $a = new NormalUser(); echo "这个类" . $a . "的实例"; ?>
abstract abstract method
Use abstract-modified classes to indicate that this method is an abstract method.
Abstract methods only have the declaration part of the method and no method body.
Abstract methods do not have {}, but end with ;.
As long as there is an abstract method in a class, the class must be declared as an abstract class.
Abstract methods must be overridden in subclasses.
The following is an abstract class with two abstract methods, setSal() and getSal(). Used to retrieve $sal employee wages.
<?php abstract class User { protected $sal = 0; //这里定义的抽象方法。 //注意抽象方法没有方法体,而且方法结束使用 ; 号。 abstract function getSal(); abstract function setSal(); //定义它的__tostring方法 public function __toString() { return get_class($this); } } ?>
<?php abstract class User { protected $sal = 0; //这里定义的抽象方法。 //注意抽象方法没有方法体,而且方法结束使用 ; 号。 abstract function getSal(); abstract function setSal(); //定义它的__tostring方法 public function __toString() { return get_class($this); } } class NormalUser extends User { } ?>
<?php abstract class User { protected $sal = 0; //这里定义的抽象方法。 //注意抽象方法没有方法体,而且方法结束使用;号。 abstract function getSal(); abstract function setSal(); //定义它的__tostring方法 public function __toString() { return get_class($this); } } class NormalUser extends User { function getSal() { } function setSal($sal) { } } //这样就不会出错了。 ?>
Line 19, missing parameters.
20 lines, more parameters.
Line 21, the parameter type is wrong. (This way of writing will be introduced in a later chapter)
If there is an abstract method in a class, the class must be declared as an abstract class.
The following class is not an abstract class. It defines an abstract method and will report an error.
<?php class User { protected $sal = 0; //这里定义的抽象方法。 //注意抽象方法没有方法体,而且方法结束使用;号。 abstract function getSal(); abstract function setSal(); //定义它的__tostring方法 public function __toString() { return get_class($this); } } //这个类中有两个抽象方法,如果这个类不是抽象的。会报错 ?>
Abstract class inherits abstract class
When an abstract class inherits another abstract class, there is no need to rewrite the abstract methods in it.
In an abstract class, the abstract method of the abstract parent class cannot be overridden.
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 rewrite the abstract methods in it.
<?php abstract class User { protected $sal = 0; abstract function getSal(); abstract function setSal($sal); } abstract class VipUser extends User { } ?>
After an abstract class is inherited, the abstract methods in it cannot be overridden.
If rewriting occurs, the system will report an error.
<?php abstract class User { protected $sal = 0; abstract function getSal(); abstract function setSal($sal); } abstract class VipUser extends User { abstract function setSal(); } ?>
Abstract classes inherit abstract classes for the purpose of extending abstract classes.
<?php abstract class User { protected $sal = 0; abstract function getSal(); abstract function setSal($sal); } abstract class VipUser extends User { protected $commision = 0; abstract function getCommision(); abstract function setCommision(); } ?>
In PHP5.1, static abstract methods are supported in abstract classes. In the example below, we see that static abstract methods can be declared. When implementing this method, it must be a static method.
Static abstract methods
In PHP5.1, static abstract methods are supported in abstract classes. In the example below, we see that static abstract methods can be declared. When implementing this method, it must be a static method.
<?php abstract class User { protected static $sal = 0; static abstract function getSal(); static abstract function setSal($sal); } class VipUser extends User { static function getSal() { return self::$sal; } static function setSal($sal) { self::$sal = $sal; } } VipUser::setSal(100); echo "you sal is " . VipUser::getSal(); ?> //这里的抽象方法好像没有问题
Reposted from: http://blog.csdn.net/klinghr/article/details/5212952
The above introduces the PHP class example tutorial: abstract class and abstract method, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.