Php object-oriented syntax 3 inherits extends

Source: Internet
Author: User
Inheritance: If an object A uses A member of another object B, we will say that object A inherits object B! Tip: The Inheritance concept is embodied in the object, and the syntax is embodied in the class classBextendsA {}! Inheritance: If an object A uses A member of another object B, we will say that object A inherits object B!
Tip: The Inheritance concept is embodied in objects, and the syntax is embodied in class B extends {}!

 GoodsName = $ goodsName; echo $ this-> goodsName;} class Books extends Goods {public function sayPrice ($ price) {$ this-> price = $ price; echo $ this-> price. 'RMB' ;}$ book1 = new Books; $ book1-> sayName ('php developer'); $ book1-> sayPrice ('2017. 156. 47 ');

The syntax indicates the reuse of code in the object-oriented syntax!
Instanceof, whether it is a certain type of instance. (Instanceof is the same as +-*/and is an operator)

 

Calculation result:
Bool (true)
Bool (true)


Override. It is a phenomenon. only inheritance occurs (use or avoid this situation)
If a member (attribute, method) with the same name exists in the subclass and parent class, only the Member defined in the subclass is obtained when the subclass object is instantiated!
Tip:
Rewrite is not a replacement!
Two different sayprices exist. We can use the Book object to get the attributes or methods we currently see, which is similar to the process of searching up to the nearest point.

  Price = $ price; echo $ this-> price. 'No monetary unit ';} class Books extends Goods {public function sayPrice ($ price) {$ this-> price = $ price; echo $ this-> price. 'RMB' ;}// $ good = new Goods; // $ good-> sayPrice ('96. 47 '); echo'
  '; $ Book1 = new Books; $ book1-> sayPrice ('2017. 47 ');

Running result:

156.47 RMB

Parent, parent class
Once overwritten, the parent class code will not be executed!
The method with the same name of the parent class subclass will be overwritten, so some methods will be overwritten, such as the constructor method!

  Goods_name = $ name; $ this-> goods_price = $ price;} class Book extends Goods {public $ author; // author public $ publisher; // Press public function _ construct ($ name, $ price, $ author, $ publisher) {parent: _ construct ($ name, $ price ); $ this-> author = $ author; $ this-> publisher = $ publisher; }}$ book1 = new Book ('phpphpphp', 88.8, 'new ', 'bejing her '); var_dump ($ book1 );

Calculation result:

object(Book)#1 (4) { [“author”]=> string(3) “new” [“publisher”]=> string(17) “Bejjing publisher” [“goods_name”]=> string(9) “phpphpphp” [“goods_price”]=> float(88.8) }

The constructor of the parent class is just a common method in the subclass.

The above is the content of The extends extension inherited by php object-oriented syntax 3. For more information, see PHP Chinese website (www.php1.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.