Class inheritance in PHP, PHP inheritance Relationship _php Tutorial

Source: Internet
Author: User

The inheritance relationship of classes in PHP, the PHP inheritance relationship


In PHP, I often write a class, the class writes a common method, and then let the subclass to inherit to get the corresponding function. Let's say there's a parent class roughly:

1 
 
   PHP 2 class father{3  4 public     function __construct () {5         echo ' I am the constructor of the parent class! ')     } 7  8     protected function say ($str = ' Hello world! ' {9         echo ' said a word: '. $str}12?>    

Then, make a subclass to inherit him:

1 
 
   PhP2 3 class Chirld extends father{4 5     public __construct () {6         echo ' I am the construction method of the subclass ~ ' C25/>}8}  

At this point you instantiate the subclass, and the result will be 我是子类的构造方法~ !
Of course, because of the 继承 relationship, the subclass is a method that can call the parent class say() .

If you want to implement the constructor of the parent class, but you want to implement the subclass constructor, you can do it this way:

1 
 
   PHP 2  3 class Chirld extends father{4  5     public __construct () {6         Parent::__ Construct (); 7         Echo ' I am a subclass of construction method ~ '    }10?>    

This will output 我是父类的构造方法! and 我是子类的构造方法~ .

Suppose you define such a method in a subclass:

1     protected function say ($str = ") {2         echo ' I am a subclass of say '; 3     } 

Then, the method you inherited from the parent class sys() will be rewritten, so the result of the call is: 我是子类的say .

What happens if you define that?

1 public     function say ($str = ') {2         echo ' I am a subclass of method Oh ~ '; 3     } 

This kind of writing is still possible. PHP differs from other strongly typed languages in that the PHP rewrite method allows you to "go public" with the overridden method, without allowing you to "privatize down", as you would say in the case where you private would definitely have an error, whereas a language like C + + would be the opposite.

PHP's idea of this situation is that your father gave you a protected level of inheritance, at this point in your hand is the protection level, your father allows you to share with others, that is public , but do not allow you to hide, that is, you have to swallow private certainly not.

At this point you are sure to ask, so say() what happens to the parent class private ?
The result is that the parent class has been privatized and the subclass cannot inherit at all, so your method in the subclass say() will be as much as you can.

Another problem is that the parent class has defined the sys() optional parameters in the band, so what if you have no parameter definitions in the subclass like this?

    Public function say () {        echo ' haha. I have no parameters, yes ~ ';    } 

The results can still work, but there are E_STRICT levels of hints. The reason is that the PHP standard is that the number of parameters must be aligned with the parent class, and of course the error level can be set in php.ini.

Above if there is any mistake place, also please correct in time, thank you.

http://www.bkjia.com/PHPjc/1033980.html www.bkjia.com true http://www.bkjia.com/PHPjc/1033980.html techarticle The inheritance of classes in PHP, PHP inheritance in PHP, I often write a class, the class writes a common method, and then let the subclass to inherit to get the corresponding function. Suppose there is roughly this ...

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