PHP class abstract and final, class method abstract and final, interface

Source: Internet
Author: User
class modifier

Final

When final is applied to a class, this class is qualified as a non-inheritable class, that is, other classes cannot inherit this class, and the final class, when you do not want others to inherit their own written class, only need to precede the final keyword to

Final class MyClass {    //code}//fatal Error final class MyClass can not is inherited by other Classclass subclass Exten DS Myclass {}

When final acts on a class method, this method is qualified to be non-overridable, that is, the subclass cannot override this method, is not inheritable, the class method can be inherited is still the public protected private qualification, the final method is only qualified subclasses cannot override this method, Even if the parent class does not inherit from the quilt class, the subclass still cannot override this method, that is, the inheritance mechanism of PHP is to first check whether this method is final, if this method is still overridden in the final subclass, the error will be further based on the public only if it is not final Protected private to limit whether subclasses can inherit this method

Class MyClass {    final public function finalpublic () {        echo __method__;    }        Final Private Function Finalprivate () {        echo __method__;    }        Final protected function finalprotected () {        echo __method__;    }} A method Myclass::finalpublic () myclass::finalprotected () myclass::finalprivate () that can be called by a parent class is not inherited by class Correctsubclass Extends Myclass {}//error class Errorsubclass extends Myclass {public    function finalpublic () {[)} because the final modified method of the parent class is overridden. C10/>echo __method__;    }    Although it is not possible to inherit the Finalprivate () method from the parent class MyClass, the    private function finalprivate () {        echo __method__ will still be given an error because it is final decorated. ;    }        protected function finalprotected () {        echo __method__;    }}


Abstract

When abstract acts on a class, this class is qualified as an abstract class and can only be used for inheritance, and cannot instantiate an object.

Abstract classes do not necessarily contain abstract methods, but abstract methods must exist in abstract classes, and subclasses inheriting from this abstract class must all implement their abstract methods, otherwise, subclasses are identified as abstract classes.

Abstract methods are required to implement the quilt class, so you can not use the private modifier to limit abstract abstract methods, can be modified with public and protected.

The abstract class MyClass {    //abstraction method is defined similar to a function declaration in C + + in which there is no function body that must be implemented in subclasses of this class, otherwise subclasses must also be abstract public    function Abstractfunc ();    Abstract public Function Abstractfuncother ();        Public Function Index () {        }}abstract class Subabstract extends MyClass {    //The abstract method that does not implement the parent abstract class must itself be an abstract class}// Otherwise implement all abstract methods class Sub extends MyClass {    //Inherit abstract class if itself is not an abstract class must implement all abstract methods of its parent class public    function Abstractfunc () {            } Public        function Abstractfuncother () {            }}

Instance Summary

 ';} Final protected function fianlprotected () {echo __method__. ' Can is inherited, but is overrided '. '
';} Final Private Function finalprivate () {echo __method__. ' Can ' is inherited or be overrided '. '
';}} Class Methodclass extends Finalmethod {public Function index () {$this->finalpublic (); $this->fianlprotected ();} Final qualification cannot override all fianl methods of the parent class regardless of whether it is inheritable from the parent class//Public function finalprivate () {//}}echo ' ==================== final ========= =============' . '
'; $method = new Methodclass (); $method->index ();//============================================================= ===========================================================================//================================== ========== abstract class and abstract method ================================================/** * Abstract class can only be used to inherit non-instantiated objects and draw Abstract methods such as abstract methods must exist in abstract classes abstract methods There is no function body similar to a function declaration in C + + * Inheriting the subclass of this abstract class or all the abstract methods in this class are implemented otherwise subclasses must also be abstract class * Abstract methods can not be decorated with private Because its own starting point is to be rewritten to achieve the exact opposite of final not allowed to be overridden abstract must be overridden so private in the abstract class does not have any meaning * the access level of the abstract method can only be public or protected And the subclass to implement the method can only be reduced access level of the method and cannot be promoted that protected can be overridden as public but public cannot be overridden to protected * access level elevation to the declaration of the top-level abstract method as a guideline even if this method is implemented later and inherited Inheriting when overriding the secondary method still does not increase the access level can only be reduced by always lowering the */abstract class Abstractclass{//index () on the date that all abstract methods of this abstract class must have been implemented index () can only be called by object When instantiating an object, all abstract methods of this class are finally implemented Public Function index () {$this->abstractpublicfunc (); $this->abstractprotectedfunc () ;} You can not use private to modify the abstract is originally used for subclass inheritance implementation, and subclasses implement this abstract method when the access level must be less than equal to the abstract public function a defined by this classBstractpublicfunc (); abstract protected function Abstractprotectedfunc ();} /** * Only implements an abstract method in the parent abstract class, and can only be used as an abstract class using */abstract class Subabstractclass extends abstractclass{//abstract method is specifically implemented However, the access level of this method in subsequent subclasses still cannot be overridden as Protectedpublic function Abstractpublicfunc () {echo __class__. __method__. '
';}} /** * Abstract methods in the parent class have been implemented to instantiate an object */class ObjectClass extends Subabstractclass{public function Abstractpublicfunc () {echo __ method__. '
';} Here you can set the access level of this method to public but it is absolutely impossible for privatepublic function Abstractprotectedfunc () {echo __method__. '
';}} Echo ' ====================abstract===================== '. '
'; $abstract = new ObjectClass (); $abstract->index ();//========================================================= ===============================================================================//============================== The ========================== Interface Interface ====================================================/** * interface is used to define a set of behavioral Any class that uses this interface must trace the method standard declared by the interface is a set of standard classes that do not need to use the abstract adornment but are all abstracted methods */interface Myinterface{public function GetName ();p ublic function Getage ();p ublic function Getsex ();} Class User implements Myinterface{private $name = null;private $age = null;private $sex = null;public function __construct ($name, $age, $sex) {$this->name = $name; $this->age = $age; $this->sex = $sex;} Public Function GetName () {echo $this->name. '
';} Public Function Getage () {echo $this->age. '
';} Public Function Getsex () {echo $this->sex. '
';}} $user = new User (' sallency ', ' Male '); Echo ' ====================interface===================== '. '
'; $user->getname (); $user->getage (); $user->getsex ();? >
  • 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.