SoFunction
Updated on 2025-03-10

Detailed explanation of classes and objects in php (inheritance)

Introduction

In php, inheritance of types uses the extends keyword, and can only inherit one parent class at most. 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 coverage

Including the constructor, a subclass can redefine class methods with the same name to override the parent class method. Follow the following rules when covering:

1. Except for the constructor, the parameter list of the function must be the same when overwriting other functions.

2. After the method is overwritten, the parent class method will not be automatically called when calling the subclass method.

3. If the parent class wants to prohibit the method from being overwritten by the child class, you can use final to declare the method. At this time, if the child class still wants 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 parent class method  $this->num = "0"; 
  $this->str = $str; 
  echo parent::getName()."\n";    // Call parent class method } 
 public function getName() { 
  return parent::getName()."$this->str\n"; // Call parent class method } 
} 
$b = new MySubClass("myName", true);  // myName 
echo $b->getName();          // myName1 
class MyClass  
{ 
 final public function getName() { 
 } 
} 

Property redefinition

In a subclass, the public and protected attribute members in the parent class can be accessed. Unless the property of the same name is redefined, the attributes in the parent class will be inaccessible.

The methods are different. After the subclass overrides 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"; 
  // Inherit the $a attribute in the parent class and use it directly  echo "\$a:$this->a; \$b:$this->b; \$c:$this->c;\n"; 
  // Call the method of the same name in the parent class  parent::f1(); 
  // Inherit the f2() method in the parent class and use it directly  $this->f2(); 
 } 
 // The parent class's 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 redefining the attribute of the parent class (the same name), the accessibility of the attribute can become more open, but cannot be stricter. That is to say, the public attribute in the parent class cannot be modified to the private attribute in the subclass.

If the parent class method is called through a child class object, when the parent class method accesses the property, for the redefined attributes of the same name, the public and protected attributes will access the subclass version, and the private attribute will access the parent class version. It can also be understood that public and protected attributes can be redefined (the version of the parent class is redefined, so it does not exist), while private is not redefined (the attributes in the parent class still exist and are accessed through the parent class method, and have nothing to do with whether there are attributes with the same name in the child class).

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 parsing operator::

Colons are also often used to access class constants and class static variables, and are 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 variables public function put() {  // Method to overwrite the subclass  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 constants  echo self::$id1 . "\n";    // Subclass variables  echo static::Name1 . "\n";  // Subclass common sense  echo static::$id1 . "\n";   // Subclass variables } 
} 
$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 parent classes in subclasses, you should avoid using parent class name directly, but use parent:: to avoid destroying the encapsulation of the parent class.

final

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

// Classes declared as final cannot be inheritedfinal 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, so there is no need to declare the method as final final public function getDat() { 
  return $this->dat; 
 } 
}

Summarize

The above is the classes and objects (inheritance) in PHP introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!