Abstract classes and interfaces for PHP

Source: Internet
Author: User

Abstract classes are similar to interfaces, and are very special classes. An abstract class is a special class, and an interface is a special kind of abstract class. They are typically used in conjunction with object-oriented polymorphism. While it is easier to declare and use them, their role is a bit more difficult to understand.

① Abstract class

In the OOP language, a class can have one or more subclasses, and each class has at least one public method that accesses its interface as an external code. Abstract methods are introduced to facilitate inheritance. This section introduces the declaration of abstract classes and abstract methods, and then explains their purpose. Before declaring an abstract class, let's look at what an abstract method is. Abstract methods are methods that do not have a method body, so-called no method body refers to the method declaration without the curly braces and its contents, but in declaring the method directly after the method name with a semicolon end. In addition, when declaring an abstract method, you also use the keyword abstract to decorate it. The format for declaring an abstract method is as follows:

Abstract function fun1 (); You can't have curly braces, you can't have content in the method body.
Abstract function fun2 (); Directly following the parentheses of the method name and ending with a semicolon, and using the abstract modifier

As long as there is a method when declaring the class abstract method, then this class is abstract class, abstract class also use the abstract keyword to decorate. You can have member methods and member properties that are not abstract in an abstract class, but access permissions cannot be decorated as private with the private keyword. The following example declares two abstract methods say () and eat () in the person class, and the person class is an abstract class that needs to use the abstract identity. The code looks like this:

123456789101112131415161718 <?php    abstract class Person{        protected $name;        protected $country;        function __construct($name="",$country="china"){            $this ->name = $name;            $this ->country = $country;        }        abstract function say();        abstract function eat();        function run(){            echo "使用两条腿走路<br>";        }    }?>

In the example above, an abstract class person is declared, in which two member properties, a constructor method, and two abstract methods are defined, and there is a non-abstract method. Abstract class is like a "semi-finished" class, in the abstract class there is no implementation of the abstract method, so the abstract class can not be instantiated, that is, cannot create objects, you can not directly use it. Since an abstract class is a "semi-finished" class, what does it do with abstract classes? The use of an abstract class contains an inheritance relationship, which defines a common interface for its subclasses, and its operations (which may or may be all) given to subclasses. is to use abstract classes as subclasses of overloaded templates, the definition of an abstract class is equivalent to defining a specification, which requires subclasses to obey. When subclasses inherit abstract classes, the abstract methods in the abstract classes must be implemented according to the needs of the subclasses themselves. Subclasses must implement all of the abstract methods in the parent class, otherwise there is an abstract method in the subclass, so it is an abstract class and cannot instantiate an object. In the following example, two classes are declared, each implementing the abstract class person declared in the previous example. The code looks like this:

1234567891011121314151617181920212223242526272829303132 <?php    class ChineseMan extends Person{        function say(){            echo $this ->name."是".$this->country."人,讲汉语<br>";            }        function eat(){            echo $this ->name."使用筷子吃饭<br>";        }    }        class Americans extends Person{        function say(){            echo $this ->name."是".$this->country."人,讲英语<br>";            }        function eat(){            echo $this ->name."使用刀子和叉子吃饭<br>";        }    }    $chianeseMan = new ChineseMan("高洛峰""中国");    $americans = new Americans ("alex","美国");    $chineseMan ->say();    $chineseMan ->eat();    $americans ->say();    $americans ->eat();?>

In the previous example, two classes were declared to inherit the abstract class person, and the abstract methods in the person class were implemented separately according to their requirements, so that two subclasses could create objects. Abstract class person can be regarded as a template, the abstract method in the class does not implement itself, but the subclass must have a parent class declaration of the abstract method, and to follow their own characteristics to implement the content of the abstract method.

② Interface Technology

Because PHP only supports single inheritance, each class can inherit only one parent class. When the declared new class inherits the abstract class implementation template, it cannot have any more parent classes. To solve this problem, PHP introduces an interface. An interface is a special kind of abstract class, and abstract class is a special class. If all the methods in an abstract class are abstract methods, we can use the "interface" technique in exchange for another declarative approach. The method declared in the interface must be an abstract method, and the variable cannot be declared in the interface, only the member property of the Const keyword is declared as a constant, and all members of the interface must have public access. The declaration of a class is identified using the "class" keyword, while the declaration of the interface is identified by using the "interface" keyword. The format of the Declaration interface is as follows:

123456 <?phpinterface接口名称{ //使用interface关键字声明接口常量成员 //接口中的成员属性只能是常量,不能是变量抽象方法 //接口中的所有方法必须是抽象方法,不能有非抽象的方法存在}?>

All methods in an interface require an abstract method, so it is not necessary to use the abstract keyword to identify it before the method. There is also no need to explicitly decorate with public access in the interface, because the default permissions are publicly and can only be common. In addition, interfaces and abstract classes cannot instantiate objects, which is a stricter specification and needs to be implemented by subclasses. However, you can use the interface name directly outside the interface to get the value of a constant member. An example of an interface declaration, the code is as follows:

1234567 <?phpinterfaceone{const CONSTANT = ‘CONSTANT value‘;function fun1();functionfun2();}?>

You can also use the Extends keyword to allow an interface to inherit from another interface, implementing an extension between interfaces. In the following example, it is declared that a two interface inherits the one interface in the previous example. The code looks like this:

123456 <?phpinterfaceTwo extends one{function fun3();functionfun4();}?>

If you need to use a member of an interface, you need to implement all the abstract methods in the interface through subclasses, and then create the object of the subclass to invoke the method implemented in the subclass. However, it is necessary to use the Implements keyword to implement the interface through the class, rather than using the extends keyword. If you need to use an abstract class to implement some of the methods in an interface, you also need to use the Implements keyword implementation. In the following example, declare an abstract class three to implement some of the methods in the one interface, but to instantiate an object, the abstract class also needs subclasses to implement all of its abstract methods. Declare a four class to implement all the methods in the one interface. The code looks like this:

1234567891011121314151617181920212223242526 <?phpinterface one{const CONSTANT = ‘CONSTANT value‘;function fun1();function fun2();}//声明一个抽象类去实现接口One中的第二个方法abstract class Three implements One{ //只实现接口中的一个抽象方法function fun2(){//具体的实现内容由子类自己决定}}//声明一个类实现接口One中的全部抽象方法class Four implements One{function fun1(){//具体的实现内容由子类自己决定}function fun2(){//具体的实现内容由子类自己决定}}?>

PHP is single-inheritance, a class can have only one parent class, but a class may implement multiple interfaces. The multiple interfaces that will be implemented are separated by commas, and the subclass Chinese Medicine implements all the abstract methods in all interfaces to create the object. The equivalent of a class to comply with multiple specifications, as we are not only to comply with national laws, if it is in schools, but also to comply with school rules. The format for implementing multiple interfaces is as follows:

Class name implements interface one, interface two, ... Interface n{
Implementing an abstract method in all interfaces
}

Implementing multiple interfaces is using the "implements" keyword, and you can also use the "extends" keyword to inherit a class. That is, to implement multiple interfaces while inheriting a class, but be sure to use extends to inherit a class, and then use inplements to implement multiple interfaces. Use the format as follows:

Class Name extends parent class name implements interface one, interface two, ... Interface n{
Implementing an abstract method in all interfaces
}

In addition to some of the above applications, there are many places to use the interface, for example, some of the developed systems, the structure of the larger adjustment is not very realistic, you can customize some interfaces and append the corresponding implementation to complete the expansion of the functional structure.

>> This article fixed link: http://php.ncong.com/php_course/oop/abstract.html

>> reprint Please specify: Ntshongwana PHP July 25, 2014 Yuncon PHP Learning tutorial Published

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.