Explore PHP Object-oriented, patterns and practices-advanced features (II)

Source: Internet
Author: User

Abstract class

Abstract classes cannot be instantiated directly. Abstract classes only define (or partially implement) the methods that a subclass requires. Subclasses can inherit it and make the abstract class materialized by implementing an abstract method in it. You can define an abstract class with the abstract keyword. In most cases, an abstract class contains at least one abstract method. Abstract methods are declared with the abstract keyword, which cannot have specific content.

Abstract class shopproductwriter{    protected $products = Array ();        Public Function addproduct (shopproduct $shopProduct)    {        $this->products[] = $shopProduct;    }    Abstract public Function write ();}

After you create an abstract method, make sure that the method is implemented in all subclasses, but the details of the implementation can be indeterminate first. Each subclass must implement all the abstract methods in the abstract class, or declare themselves as abstract methods. The extension class is not just responsible for simply implementing the methods in the abstract class, but it must also re-declare the methods. The access control of the new implementation method cannot be more restrictive than the access control of the abstract method. The number of parameters of the new implementation method should be the same as the number of parameters of the abstract method, regenerate the corresponding type hint.

Class Xmlproductwriter extends shopproductwriter{public    function write ()    {        $str = ' <?xml version= ' 1.0 ' encoding= "UTF-8"?> '. " \ n ";                $str. = "<products>\n";                foreach ($this->products as $shopProduct) {                    $str. = "\t<product title=\" {$shopProduct->gettitle ()}\ "> \ n ";                    $str. = "\t\t<summary>\n";                    $str. = "\t\t{$shopProduct->getsummaryline ()}\n";                    $str. = "\t\t</summary>\n";                    $str. = "\t</product>\n";        }                $str. = "</products>\n";                Print $str;    }} Class Textproductwriter extends shopproductwriter{public    function write ()    {        $str = "products:\n";                foreach ($this->products as $shopProduct) {                    $str. = $shopProduct->getsummaryline (). " \ n ";        }                Print $str;    }}

Interface

Abstract classes provide specific implementation criteria, while interfaces (interface) are purely templates. An interface can only define functionality, not the content of the implementation. The interface can be declared with the keyword interface. An interface can contain property and method declarations, but the method body is empty.

Interface chargeable{public    function GetPrice ();}

Any class that implements an interface implements all of the methods defined in the interface, otherwise the class must be declared abstract. A class can use the Implement keyword in a declaration to implement an interface.

Class Shopproduct implements chargeable{    //...    function GetPrice ()    {        return ($this->getprice-$this->discount);    }    //...}

The Shopproduct class already has a GetPrice () method, so is it useful to implement the chargeable interface? The answer is yes, because of the type. The class that implements the interface accepts the class it inherits from and the type of interface it implements.

Any class can implement an interface, and an interface can effectively join unrelated types. A class can inherit a parent class and implement any interface at the same time. The extends clause is preceded by a implements clause.

Class Consultancy extends Timedservice implements Bookable, chargeable{//...}

Lazy static binding: Static keyword

The most obvious sign of this feature is the new keyword static. Static is similar to self, but it refers to the class being called instead of the containing class. In this case, it means that calling document::create () will generate a new Document object instead of trying to instantiate a DomainObject object.

Abstract class domainobject{public    static function create ()    {        return new static ();    }} Class User extends domainobject{   }class document extends Domainobject{}print_r (Document::create ());//Output as document object//      (//      )

The static keyword can be used not only for instantiation. Like self and parent, static can also be invoked as an identifier for a static method call, even from a non-static context.

abstract class domainobject{private $group;    Public Function construct () {$this->group=static::getgroup ();    public static function Create () {return new static ();    } static function Getgroup () {return ' default '; }}class User extends domainobject{}class Document extends domainobject{static function Getgroup () {return ' d    Ocument ";          }}class SpreadSheet extends Document{}print_r (user::create ());p Rint_r (Spreadsheet::create ());//output is User object//(// [group:domainobject:private]=>default//]//SpreadSheet object//(//[Group:domainobje ct:private]=>document//) 

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.