Basic Usage Example of PHP Abstract Class
- 2021-11-13 06:52:05
- OfStack
This article illustrates the basic usage of PHP abstract classes. Share it for your reference, as follows:
<?php
// Abstract class image 1 Templates for subclass extension ( Rewrite ) There are common methods in abstract classes ( Methodological body ) There are also abstract methods, where abstract methods have no method body and end with semicolons.
// As long as there is 1 The class must be defined as an abstract class.
// An abstract class cannot create an instance of itself, and when trying to create an abstract class object, a 1 A result of mistakes.
// Implement methods of abstract classes in subclasses, and the visibility should be equal to and higher than the visibility of abstract methods. Abstract classes (public|protected|private) , subclass (public|public,protected| Has a fatal error and cannot be inherited ) .
// Abstract classes cannot define private methods, but can define private properties.
// If the subclass does not extend ( Rewrite ) Abstract methods of abstract classes will cause errors.
abstract class Employees{
protected $name = null;
function __construct($nameStr){
$this->name = $nameStr;
}
abstract public function work();
public function work1(){
echo "<p>$this->name</p>";
}
//function __destruct(){}
}
class Managers extends Employees{
public function __construct($nameStr){
parent::__construct($nameStr);
}
/*public function work(){// If the subclass does not extend ( Rewrite ) Abstract methods of abstract classes will cause errors.
echo "<p>$this->name is working</p>";
}*/
}
//$obj1 = new Employees();// When an abstract class object is created, a 1 A result of mistakes.
$obj2 = new Managers('e2');
//$obj2->work();
Running the above code will prompt the following error message:
Fatal error: Class Managers contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Employees::work) in D:\phproot\test\ClassDemo.php on line 26
After deleting the error prompt position and the end position code comment, change it to the following code:
<?php
// Abstract class image 1 Templates for subclass extension ( Rewrite ) There are common methods in abstract classes ( Methodological body ) There are also abstract methods, where abstract methods have no method body and end with semicolons.
// As long as there is 1 The class must be defined as an abstract class.
// An abstract class cannot create an instance of itself, and when trying to create an abstract class object, a 1 A result of mistakes.
// Implement methods of abstract classes in subclasses, and the visibility should be equal to and higher than the visibility of abstract methods. Abstract classes (public|protected|private) , subclass (public|public,protected| Has a fatal error and cannot be inherited ) .
// Abstract classes cannot define private methods, but can define private properties.
// If the subclass does not extend ( Rewrite ) Abstract methods of abstract classes will cause errors.
abstract class Employees{
protected $name = null;
function __construct($nameStr){
$this->name = $nameStr;
}
abstract public function work();
public function work1(){
echo "<p>$this->name</p>";
}
//function __destruct(){}
}
class Managers extends Employees{
public function __construct($nameStr){
parent::__construct($nameStr);
}
public function work(){// If the subclass does not extend ( Rewrite ) Abstract methods of abstract classes will cause errors.
echo "<p>$this->name is working</p>";
}
}
//$obj1 = new Employees();// When an abstract class object is created, a 1 A result of mistakes.
$obj2 = new Managers('e2');
$obj2->work();
Test run results:
e2 is working
More readers interested in PHP can check out the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"
I hope this article is helpful to everyone's PHP programming.