PHP class example tutorial: abstract class and abstract method

WBOY
Release: 2016-08-08 09:26:37
Original
848 people have browsed it

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();
?>
Copy after login
The NormalUser in the example below inherits from the User class and can be instantiated.
<?php
//定义一个抽象类
abstract class User
{
	public function __toString() {
		return get_class($this);
	} 
}
//实例化这个类会出现错误
echo new User();
class NormalUser extends User
{
}
$a = new NormalUser();
echo "这个类" . $a . "的实例";
?>
Copy after login
It makes no sense to set up an abstract class alone. Only with abstract methods can abstract classes have flesh and blood. Abstract methods are introduced below.

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);
	} 
} 
?>
Copy after login
Since the User class cannot be directly inherited, we write a NormalUser class that inherits from the User class. When we write the following code, the system will report an error. This error tells us that there are two abstract methods in the User class and we must override these two methods in the subclass.
<?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
{
}
?> 
Copy after login
The following example rewrites these two methods. Although the content of {} in the method body is empty, it can be regarded as rewriting the method. Pay attention to the parameter names of the overridden method. As long as the number of parameters is the same, it is not required that the parameter names must be the same.
<?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) { 
	}
}
	//这样就不会出错了。
?>
Copy after login
In lines 19-21 below, the three methods of writing and rewriting will report errors.

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);
	} 
}
	//这个类中有两个抽象方法,如果这个类不是抽象的。会报错
?>
Copy after login

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  
{  
}  
?>  
Copy after login

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(); 
}
?>
Copy after login

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();
}
?>
Copy after login

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();
?>
//这里的抽象方法好像没有问题 
Copy after login

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.

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