Php class and object_php tutorial

Source: Internet
Author: User
Php class and object. Php class and object-oriented programming are the mainstream today. for R & D personnel, there may be more or less understanding of object-oriented, however, some of the less commonly used php classes and objects may not be particularly clear.

Object-oriented is the mainstream of today's programming. for R & D personnel, there may be more or less understanding of object-oriented, but some of the less commonly used ones may not be particularly clear. It is sometimes useful. The following describes some knowledge.


I. knowledge about final Keywords:


1. the final keyword can be inherited by the quilt class as a method. As shown below:

class A{    final function operation(){        echo 'a';    }}class B extends A{}$a=new B();$a->operation();

Result:


2. the final keyword as a class cannot be inherited, as shown below:


 operation();


The following error occurs:

(! ) Fatal error: Class B may not inherit from final class (A) in D: \ wamp \ www \ examble \ index19.php on line9


3. the final keyword as a method cannot be overwritten by the quilt class, that is, the subclass cannot have the same method, as shown below:

 class A{    final function operation(){        echo 'a';    }} class B extends A{     function operation(){        echo 'a';    }}$a=new B();$a->operation();

The following error occurs:

(! ) Fatal error: Cannot override final method A: operation () in D: \ wamp \ www \ examble \ index19.php on line12


2. php multi-inheritance implementation. the following example will cause a fatal error in php.

 class A{    public  function operation(){        echo 'a';    }}class C{    public function oper(){        echo 'c';    }} class B extends A{   public  function operation(){        echo 'a';    }} class B extends C{   public function operati(){      echo 'd';   }}$a=new B();$a->operation();

(! ) Fatal error: Cannot redeclare class B in D: \ wamp \ www \ examble \ index19.php on line24


Multiple inheritance types in this form are not allowed.


If you have to implement multiple types of inheritance, you can only implement it through interfaces.


 interface Displayable{   public function display(); }  interface B{   public function show(); }  class A implements Displayable,B{   public function display(){     echo 'a';   }   public function show(){     echo 'b';   } }  $ab=new A(); $ab->display(); $ab->show();

Note that the interface methods are all public, the interface methods only have methods, there is no method body, the subclass overrides the interface method, the interface methods must be overwritten in the subclass.


Http://www.bkjia.com/PHPjc/941436.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/941436.htmlTechArticlephp class and object-oriented, is the mainstream of today's programming, for R & D personnel, may have some understanding of object-oriented, more or less, but some not commonly used may not be particularly clear...

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.