Describes classes and objects (inheritance) in php and php

Source: Internet
Author: User

Describes classes and objects (inheritance) in php and php

Introduction

In php, The extends keyword is used for inheritance of types, and only one parent class can be inherited. php does not support multi-inheritance.

class MyClass  {  public $dat = 0;  public function __construct($dat) {   $this->dat = $dat;  }  public function getDat() {   return "$this->dat\n";  } } class MySubClass extends MyClass {  public function getDat() {   return "dat: $this->dat\n";  } } $a = new MyClass(3); $b = new MySubClass(4); echo $a->getDat();  // 3 echo $b->getDat();  // dat: 4 

Method coverage

Including constructors, sub-classes can redefine class methods with the same name to overwrite the parent class methods. Follow the following rules when overwriting:

1. Except constructors, when other functions are overwritten, the function parameter list must be the same

2. After a method is overwritten, including the constructor, the parent class method is not automatically called when the subclass method is called.

3. If the parent class wants to disable method overwriting by the quilt class, you can use final to declare the method. If the subclass still needs to overwrite the parent class method, an error will occur.

Class MyClass {private $ name = ""; public $ num = 0; public $ str = ""; public function _ construct ($ name) {$ this-> name = $ name; $ this-> num = 100; $ this-> str = "none";} public function getName () {return $ this-> name ;}} class MySubClass extends MyClass {public function _ construct ($ name, $ str) {parent ::__ construct ($ name ); // call the parent class method $ this-> num = "0"; $ this-> str = $ str; echo parent: getName (). "\ n"; // call the parent class method} public function getName () {return parent: getName (). "$ this-> str \ n"; // call the parent class method} $ B = new MySubClass ("myName", true ); // myName echo $ B-> getName (); // myName1 class MyClass {final public function getName (){}}

Attribute redefinition

In the subclass, you can access the public and protected attribute members in the parent class. Unless you redefine a self-owned attribute with the same name, the attributes in the parent class cannot be accessed.

The method is different. After the subclass overwrites the method, the parent class method can still be accessed.

Class MyClass {public $ a = 1; protected $ B = 2; private $ c = 3; public function f1 () {echo "MyClass f1 \ n "; echo "\ $ a: $ this-> a; \ $ B: $ this-> B; \ $ c: $ this-> c; \ n ";} protected function f2 () {echo "MyClass f2 \ n"; echo "\ $ a: $ this-> a; \ $ B: $ this-> B; \ $ c: $ this-> c; \ n ";}private function f3 () {echo" MyClass f3 \ n ";}} class MySubClass extends MyClass {public $ B = 22; public $ c = 33; public function f1 () {echo "MySubClass f1 \ n"; // inherits the $ a attribute from the parent class and directly uses echo "\ $: $ this-> a; \ $ B: $ this-> B; \ $ c: $ this-> c; \ n "; // call the parent method with the same name in the parent class:: f1 (); // The f2 () method inherited from the parent class. use $ this-> f2 ();} // The f3 () of the parent class is private, the definition here has nothing to do with the parent class public function f3 () {echo "MySubClass f3 \ n" ;}}$ B = new MySubClass; $ B-> f1 (); echo "\ n";/* MySubClass f1 $ a: 1; $ B: 22; $ c: 33; MyClass f1 $ a: 1; $ B: 22; $ c: 3; MyClass f2 $ a: 1; $ B: 22; $ c: 3; */$ B-> f3 (); echo "\ n "; /* MySubClass f3 */

When you define a parent class (with the same name) attribute, the accessibility of the attribute can become more open, but not more strict. That is to say, the public attribute in the parent class cannot be changed to the private attribute in the subclass.

If the parent class method is called through a subclass object, the attributes of the public and protected classes will be accessed for attributes with the same name that are redefined when accessing the attribute, the private attribute will be accessed to the parent class version. It can also be understood that the public and protected attributes can be redefined (the version of the parent class is redefined and thus does not exist ), private is not redefined (the attributes in the parent class still exist and are accessed through the parent class method, which is irrelevant to whether the attributes with the same name exist in the subclass ).

Class MyClass {public $ a = 1; protected $ B = 2; private $ c = 3; public function f1 () {echo "\ $ a: $ this->; \ $ B: $ this-> B; \ $ c: $ this-> c; \ n ";}} class MySubClass extends MyClass {public $ a = 11; // It must be public protected $ B = 22; // it must be protected or public private $ c = 33; public function f2 () {echo "\ $: $ this-> a; \ $ B: $ this-> B; \ $ c: $ this-> c; \ n ";}}$ B = new MySubClass; $ B-> f1 (); // $ a: 11; $ B: 22; $ c: 3; $ B-> f2 (); // $ a: 11; $ B: 22; $ c: 33;

Range Resolution OPERATOR ::

The colon is also commonly used for class constants and class static variables, and is also used to call the parent class version when the method is overwritten. It also includes keywords such as parent, self, and static.

Class MyClass {const Name0 = "MyClass"; // class constant public static $ id0 = 0; // class variable public function put () {// Method for overwriting the quilt class echo "MyClass put () \ n" ;}} class MySubClass extends MyClass {const Name1 = "MySubClass"; public static $ id1 = 1; public function put () {parent: put (); // call the object method echo parent: Name0 of the parent class version. "\ n"; // The parent constant echo parent: $ id0. "\ n"; // echo self: Name1. "\ n"; // subclass constant echo self: $ id1. "\ n"; // subclass variable echo static: Name1. "\ n"; // subclass common sense echo static: $ id1. "\ n"; // subclass variable }}$ a = "MyClass"; $ ca = new MyClass; $ cb = new MySubClass; $ cb-> put (); echo MyClass: Name0. "\ n"; echo MyClass: $ id0. "\ n"; echo $ a: Name0. "\ n"; echo $ a: $ id0. "\ n"; echo $ ca: Name0. "\ n"; echo $ ca: $ id0. "\ n ";

When accessing members of the parent class in a subclass, avoid directly using the parent class name, instead of using parent: to avoid damaging the encapsulation of the parent class.

Final

Methods declared as final cannot be overwritten by the quilt class. If the class is declared as final, this class cannot be inherited.

// Classes declared as final cannot be inherited from final class MyClass {private $ dat; public function _ construct ($ dat) {$ this-> dat = $ dat ;} // The final method cannot be overwritten, but this class is already a final class. It is not necessary for the method to declare final public function getDat () {return $ this-> dat ;}}

Summary

The above is a small series of php classes and objects (inheritance), I hope to help you, if you have any questions, please leave a message, the small series will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.