Explanation of Class and Object of Inheritance in php

  • 2021-08-28 19:40:42
  • OfStack

Brief introduction

In php, type inheritance uses the extends keyword and can only inherit a maximum of one parent class, while php does not support multiple 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 override

Subclasses, including constructors, can redefine class methods with the same name to override parent class methods. Follow the following rules when overriding:

1. All functions except the constructor must have the same argument list when they are overridden

2. When a method is overridden, including a constructor, the parent method is not automatically called when a subclass method is called

3. If the parent class wants to prevent the method from being overridden by the child class, you can use final to declare the method. If the child class still wants to override 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 a child class, you can access the public and protected attribute members in the parent class, unless you redefine your own attribute with the same name, and then the attribute in the parent class will not be accessible.

Method is different. After the subclass overrides the method, it can still access the parent class method.


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"; 
  //  Inherited into the parent class $a Property, directly using the  
  echo "\$a:$this->a; \$b:$this->b; \$c:$this->c;\n"; 
  //  Call the method with the same name in the parent class  
  parent::f1(); 
  //  Inherited into the parent class f2() Method, using the  
  $this->f2(); 
 } 
 //  Of the parent class f3() Is private, and 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 a parent (namesake) attribute is redefined, the accessibility of the attribute can become more open, but not more strict, that is, the public attribute in the parent class cannot be modified to the private attribute in the subclass.

If the parent method is invoked through a subclass object, then when the parent method accesses the property, the properties of public and protected will access the subclass version, and the private property will access the parent version for the redefined property with the same name. It can also be understood that the public and protected attributes can be redefined (the version of the parent class is redefined so that it does not exist), while the private is not redefined (the attribute in the parent class still exists and is accessed through the parent class method, regardless of whether there is an attribute with the same name in the subclass).


class MyClass  
{ 
 public $a = 1; 
 protected $b = 2; 
 private $c = 3; 
 public function f1() { 
  echo "\$a:$this->a; \$b:$this->b; \$c:$this->c;\n"; 
 } 
} 
class MySubClass extends MyClass  
{ 
 public $a = 11;   //  Must be public 
 protected $b = 22; //  Must be protected Or public 
 private $c = 33;   
 public function f2() { 
  echo "\$a:$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::

Colons are often used to access class constants, class static variables, and to call the parent class version when a method is overridden. 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() {  //  Methods that will be overridden by subclasses  
  echo "MyClass put()\n"; 
 } 
} 
class MySubClass extends MyClass  
{ 
 const Name1 = "MySubClass"; 
 public static $id1 = 1;  
 public function put() { 
  parent::put();        //  Call the object method of the parent class version  
  echo parent::Name0 . "\n";  //  Parent class constant  
  echo parent::$id0 . "\n";   //  Parent class variable  
  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 in the parent class in a subclass, avoid using the parent class name directly, and use parent:: to avoid breaking the encapsulation of the parent class.

final

A method declared final cannot be overridden by a subclass, and if a class is declared final, the class cannot be inherited.


//  Declare as final Classes of cannot be inherited  
final class MyClass 
{ 
 private $dat; 
 public function __construct($dat) { 
  $this->dat = $dat; 
 } 
 // final Method cannot be overridden, but this class is already final Class, there is no need for methods to be declared as final It's over  
 final public function getDat() { 
  return $this->dat; 
 } 
}

Summarize


Related articles: