Home > Article > Backend Development > php classes and objects_PHP tutorial
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. 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
|
---|
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();
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...