php can define abstract methods. In order to facilitate class inheritance, the concept of abstract methods is introduced in PHP5; abstract methods are methods without method bodies. When declaring abstract methods in PHP, use the "abstract" keyword modification, and the syntax is "abstract access permission modifier function method name (parameter list) );".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php can define abstract methods.
In object-oriented languages, a class can have one or more subclasses, and each class should have at least one public method as an entry point for external code to access it.
Abstract classes and abstract methods are a concept introduced in PHP5, mainly to facilitate class inheritance.
Abstract method and abstract method
Abstract method is a method without method body. The so-called no method body refers to, When declaring a method, there are no curly braces { } and its contents. Instead, a semicolon is added directly after the method name. In addition, use the "abstract" keyword when declaring an abstract method. The format is as follows:
abstract 访问权限修饰符 function 方法名1(参数列表); abstract 访问权限修饰符 function 方法名2(参数列表);
Example:
abstract function fun1(); abstract function fun2();
The above example is the abstract method "fun1()" and "fun2" without a method body modified by "abstract" ()", don't forget there is a semicolon after the abstract method;
So what is an abstract class?
As long as a method in a class is an abstract method, then the class must be defined as an abstract class, and the abstract class must also be modified with the "abstract" keyword; there can be methods that are not abstract in an abstract class and member attributes, but as long as one method is abstract, the class must be declared as an abstract class and decorated with "abstract".
abstract class Demo { var $test; abstract function fun1(); abstract function fun2(); function fun3(){ ... } }
In the above example, an abstract class "Demo" is defined and modified with "abstract". In this class, a member attribute "$test" and two abstract methods "fun1" and " fun2", there is also a non-abstract method fun3();
So how do we use abstract classes? The most important point is that abstract classes cannot produce instance objects, so they cannot be used directly. We have mentioned many times that classes cannot be used directly. We are using objects instantiated through classes, so abstract classes cannot produce instance objects. What is the use of declaring an abstract class? We use abstract methods as templates for subclass overloading. Defining an abstract class is equivalent to defining a specification, which requires subclasses to comply. After the subclass inherits the abstract class, it Abstract methods are implemented as needed by subclasses. The subclass must implement all the abstract methods in the parent class. Otherwise, if there are still abstract methods in the subclass, then the subclass will still be an abstract class, and it will still not be able to instantiate the class; why do we have to inherit from the abstract class? Because sometimes we must inherit from an abstract class to implement some functions, otherwise you will not be able to implement these functions. If you inherit an abstract class, you must implement the abstract methods in the class;
<? abstract class Demo { var $test; abstract function fun1(); abstract function fun2(); function fun3() { ... } } $demo=new Demo(); //抽象类不能产生实例对象,所以这样做是错的,实例化对象交给子类 class Test extends Demo { function fun1() { ... } function fun2() { ... } } $test = new Test(); //子类可以实例化对象,因为实现了父类中所有抽象方法 ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of Can php define abstract methods?. For more information, please follow other related articles on the PHP Chinese website!