Home  >  Article  >  Backend Development  >  php classes and objects_PHP tutorial

php classes and objects_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:09:46763browse

php classes and objects

Object-oriented is the mainstream of programming today. R&D personnel may have some understanding of object-oriented, but some of the less commonly used ones may not be particularly clear. Sometimes it's also useful. Here are some tips.


1. Some knowledge about final keyword:


1. The final keyword as a method can be inherited by subclasses. As follows:

class A{
    final function operation(){
        echo 'a';
    }
}

class B extends A{
}


$a=new B();
$a->operation();

result :a


2. The final keyword cannot be inherited as a class, as follows:




There will be the following error:

( ! ) Fatal error: Class B may not inherit from final class (A) in D:wampwwwexambleindex19.php on line 9


3. The final keyword as a method cannot be overridden by subclasses, which means that subclasses cannot have the same method, as follows

 class A{
    final function operation(){
        echo 'a';
    }
}

 class B extends A{
     function operation(){
        echo 'a';
    }
}


$a=new B();
$a->operation();

There will be the following error:

( ! ) Fatal error: Cannot override final method A::operation() in D:wampwwwexambleindex19.php on line
( ! ) Fatal error: Cannot override final method A::operation() in D:wampwwwexambleindex19.php on line 12
12


2. Implementation of multiple inheritance in PHP. The following example will have 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:wampwwwexambleindex19.php on line 24
( ! ) Fatal error: Cannot redeclare class B in D:wampwwwexambleindex19.php on line
24

This form of multiple inheritance is not allowed.

If you have to implement multiple inheritances, you can only achieve 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 methods of the interface are all public. The methods of the interface only have methods and no method bodies. Subclasses override the methods of the interface. All methods of the interface must be rewritten in the subclass.


http://www.bkjia.com/PHPjc/941436.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/941436.htmlTechArticle
php classes and objects are object-oriented, which is the mainstream of programming today. For R&D personnel, they may be more interested in object-oriented. A few people have some knowledge, but some that are not commonly used may not be particularly clear...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn