PHP Object-oriented Syntax 5 final class, abstract class, interface structure (not Class)

Source: Internet
Author: User
The role of the class
1 Instantiation of objects
2 as the base class of other classes, is inherited!
3 calling its static members

Two main functions: instantiating objects, inheriting them.

There are two kinds of special classes, single function!
1, * * Only objects can be instantiated and cannot be inherited. **final class
2, can only be inherited, cannot instantiate an object. Abstract class

Final class and final method
The final class is the end chain on the inheritance chain and no more subclasses

Final class End{}class Test  extends end{}
Operation Result: Error PHP Fatal error:class Test may not inherit from final Class (End) in/usercode/file.php on line 7

Final method, restriction methods cannot be overridden by a quilt class

<?phpclass  amparent{    final public Function End_func () {        echo ' I am the final method ';    }} Class Amchild extends amparent{public     function End_func () {        echo ' I am a subclass, I want to rewrite the final method, but error ';    }}
Operation Result: Error PHP Fatal error:cannot override final Method Amparentt::end_func () in/usercode/file.php on line 11

Abstract class
There is one more thing than regular classes (class constants, static properties, static methods, non-static properties, non-static methods)-abstract methods that require non-abstract subclasses to implement abstract methods.
Abstract method: There is no {}, which is called only a method declaration. can have parameters.

Abstract public Function Abstract_func ($arg 1, $arg 2)
Cannot instantiate object <?phpabstract class amparent{    abstract public Function Abstract_func ($arg 1, $arg 2);} $amParent =new amparent;
Running result: PHP Fatal Error:cannot Instantiate abstract class Amparent in/usercode/file.php on line 5

can only be inherited
Two cases that are inherited:
1. Subclasses are abstract classes
2. The subclass is not an abstract class, and the abstract method of the parent class must be fully implemented (plus the method body {}).

<?phpabstract class amparent{    abstract public Function Abstract_func ($arg 1, $arg 2);} Class Amchild extends amparent{}
Run result, error: PHP Fatal error:class Amchild contains 1 abstract method and must therefore be declared abstract or implement the Remaining methods (Amparent::abstract_func) in/usercode/file.php on line 6

The case of no error:
1 plus abstract as an abstraction class

<?phpabstract class amparent{    abstract public Function Abstract_func ($arg 1, $arg 2);} Abstract class Amchild extends amparent{}

2 Implementing an abstract method

<?phpabstract class amparent{    abstract public Function Abstract_func ($arg 1, $arg 2);} class Amchild extends amparent{Public     function Abstract_func ($arg 1, $arg 2) {        echo $arg 1. $arg 2;         }} $amChild =new amchild; $amChild->abstract_func (' have implemented ', ' abstract_func ');
Operation result, error: implemented Abstract_func

Use meaning
An abstract class that specifies a method whose non-abstract subclasses must exist, but that the method body can be different.

<?phpabstract class goods{    abstract public Function Sayname (),} class Books extends  goods{     public function Sayname () {         echo ' I am the name of a book <br/> ';     }} class Mobiles extends  goods{public     function Sayname () {         echo ' I am a phone name <br/> ';     }} $book 1=new Books; $book 1->sayname (); $mobile 1=new mobiles; $mobile 1->sayname ();
Running result: I'm the name of a book I'm the name of a cell phone

Interface structure:
Used to restrict the public methods (interface methods) that a class (object) should have!

<?php    Interface igoods{        const pai=3.14;        Public function Sayname ();        Public function Sayprice ();    }

The interface can contain only public abstract methods (incomplete methods) and constants!
Implement implements
The class that implements the interface structure contains the abstract methods defined in the interface.
Two choices, 1 is defined as abstract class, 2 implements interface Method!

1 definition abstract class Books implements igoods{abstract public  function sayname ();    Abstract public  function Sayprice ();}
2 Implementing the Interface method class Books implements igoods{public     function Sayname () {         echo ' I implemented the interface method ';     }     Public Function Sayprice () {         echo ' I implemented the interface method ';     }}

Compare the difference between an abstract class and an interface:
1 An inheritance relationship between an abstract class and a normal class
The ordinary class inherits the abstract class, and the first one can get the regular members that are already in the abstract class.
The second is to implement abstract methods (not necessarily public)
2. Interface and common class is the realization of the relationship!
The common class implements the interface and can only implement it without the public method of implementation!
3. Interfaces are only used to define public methods and constants! and abstract class, anything can have!
4. Interface multi-implementation. A class that can implement multiple interfaces at the same time

<?phpinterface ia{        const pai=3.14;    Public function SayA ();} Interface ib{public    function Sayb ();} Class C implements ia,ib{public    function SayA () {    } public    function Sayb () {    }}

Tip
An interface is not a class, and an interface is a separate structure that restricts the structure of a class!

Use Class_exists (@param string) to determine if the class exists Var_dump (class_exists (' C ')); Var_dump (class_exists (' IA '));
Run Result: BOOL (TRUE) bool (false)

There is no method body and no abstract keyword public methods and constants.


The above is the PHP object-oriented Syntax 5 final class, abstract class, interface structure (not the class) of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.