PHP Advanced Abstract, Interface (interface), Trait (feature)

Source: Internet
Author: User

Abstract class

PHP 5 supports abstract classes and abstract methods. A class that is defined as abstract cannot be instantiated.

    • Abstract methods can only be in abstract classes, and abstract classes may contain non-abstract methods
    • A method that is defined as abstract simply declares its invocation method (parameter) and cannot define its specific function implementation
    • when inheriting an abstract class, the subclass must define all the abstract methods in the parent class , and the access control for these methods must be the same as (or looser) in the parent class.

Abstract methods provide us with a good way to protect the parent class.

classFruit {Private $color;  Public functioneat () {//Chew    }          Public functionSetColor ($c) {         $this->color =$c; } } classAppleextendsFruit { Public functioneat () {//Chew until Core    } } classOrangeextendsFruit { Public functioneat () {//Peel//chew    } } 

It defines a ' fruit parent ' and defines two sub-classes ' Apple ', ' Orange ', at which point

$apple New  $apple->eat ();     // instantiate the Apple class, call the Eat method, there's nothing wrong $fruit New Fruit ();      $fruit->eat ();     // If you instantiate the Friut class, it's a little strange to call the Eat method, what's does that taste like???

The abstract approach solves this problem perfectly.

Abstract class Fruit {     private$color;          Abstract  Public function eat ();           Public function setcolor ($c) {         $this$c;     

Therefore, if we need subclasses of a class to implement the same method, each method is implemented differently, you can use the abstract method

Interface

Using interfaces (interface), you can specify which methods a class must implement, but you do not need to define the specifics of these methods.

    • Only method names (with parameters) can be defined in an interface, and no method body
    • All methods defined in the interface must be public
    • To implement an interface that uses the implements operator, all methods defined in the interface must be implemented in the class
    • When designing an interface, if you are unsure of the number of method parameters, you can use ... $argumens mutable parameters instead, or directly to null, by passing in a mutable parameter in a subclass or by Func_get_args
    • A class can implement multiple interfaces at the same time, when implementing multiple interfaces, the methods in the interface cannot have duplicate names
    • Interfaces can also be inherited by using the extends operator
    • Constants can also be defined in an interface

Through the interface we can very well reduce the coupling of the Code

For example:

I have a database, I want to write a class to access my database, so I define an interface

Interface Database {function listorders ():array;    // array required for secondary method return function addorder (array$arr);      // requires the secondary method parameter type to be an array function Removeorder (); ... }

Now we use MySQL database, so I wrote a MySQL class

class Implements Database {function listorders ():array{... }

After a while, we wanted to change to Oracle database, and we wrote an Oracle database class to implement the interface.

class Implements Database {publicfunction listorders ():array{... }

Because we used the interface, all the methods (parameters, return values) are consistent, we just need to modify one line of code to complete the switch

$database New Oracledatabase ();

Trait

Starting with PHP version 5.4.0, PHP provides a new concept of code reuse, which is trait. Trait it literally means "features", "features", we can understand that using the TRAIT keyword, you can add new features to the classes in PHP.

Familiar with object-oriented knowledge, the common code reuse in software development is inherited and polymorphic two ways. In PHP, you can only implement single inheritance. and trait avoids this. The following is a simple example to illustrate the comparison.

trait Ezcreflectionreturninfo {functionGetreturntype () {/*1*/ }    functionGetreturndescription () {/*2*/ }}classEzcreflectionmethodextendsReflectionmethod { UseEzcreflectionreturninfo; /* ... */}classEzcreflectionfunctionextendsreflectionfunction { UseEzcreflectionreturninfo; /* ... */}

Declare a trait directly using the TRAIT keyword + class name, similar to declaring a class

Use trait with keyword use + class name

Note: Members that inherit from the base class are overwritten by the members that are inserted by trait. Precedence is the method from which the member of the current class overrides the trait, and trait overrides the inherited method.

PHP Advanced Abstract, Interface (interface), Trait (feature)

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.