A Simple Example of PHP Object Oriented Programming Inheritance Usage

  • 2021-11-13 06:51:04
  • OfStack

This paper illustrates the inheritance usage of PHP object-oriented programming. Share it for your reference, as follows:


<?php
// Inheritance is a subclass ( Derived class ) From parent class ( Base class, superclass ) Inherit properties and methods. 
// Subclasses can also have their own properties and methods. 
//1 A parent class can be inherited by multiple subclasses. 
// If you want to modify the method of the parent class, you can only override this method in the subclass, which is also the embodiment of polymorphism. 
// Use if($obj instanceof SomeClass){} To check 1 Whether objects belong to 1 A class. 
// If $name Yes protected , private Access permission, it will not be directly accessed outside the class. 
// If $name Yes private Access permission, it will only be accessed in its own class. 
// If $name Yes protected Access permissions, which can be accessed in their own classes or in subclasses. 
//__construct() Is the constructor of a class, which is automatically accessed when creating an object instance, and subclasses also have their own constructors. 
// When a subclass does not have a constructor, the constructor of the parent class is called when the object is created. 
// When a subclass has a constructor, it is not necessary to call the constructor of the parent class unless there is a parent When explicitly called, the constructor of the parent class is called. 
// At the end of the program, or use unset() Object, the destructor is called. 
// If the class defines final This method cannot be overridden by a subclass. 
// If the class declares final The class cannot be inherited. 
// As a convention, private variable names are usually called 1 Start with an underline. 
// If 1 A method of a class can only be called by itself, so it can be set to be protected or private. 
//$this References an instance of the current object, self Is used as the current class 1 A reference. 
// Static properties and class constants can only use class names, parent , self To access 
// Function names are case-insensitive, and variables are case-sensitive. 
class Employees{
  protected $name = null;
  public static $count = 0;
  function __construct($nameStr){
    $this->name = $nameStr;
    echo "<p>$this->name : ",self::$count," : parent : __construct</p>";
  }
  function work(){
    echo "<p>$this->name is working</p>";
  }
  
  function __destruct(){
    echo "<p>parent unset $this->name</p>";
  }
}
class Managers extends Employees{
  private $pos = null;
  function __construct($p,$nameStr){
    parent::$count++;
    parent::__construct($nameStr);
    $this->pos = $p;
    echo "<p>$this->name , $this->pos : self : __construct</p>";
  }
  function assignJob(){
    echo "<p>$this->name assign jobs</p>";
  }
  function getName(){
    return $this->name;
  }
  function __destruct(){
    echo "<p>self unset $this->name</p>";
  }
}
class Programmers extends Employees{
  function code(){
    echo "<p>$this->name is coding</p>";
  }
  function getName(){
    return $this->name;
  }
}
$e1 = new Employees('e1');
$e2 = new MAnagers(2,'e2');
$e3 = new Programmers('e3');
$e1->work();
$e2->work();
$e3->work();
$e2->assignJob();
$e3->Code();
echo "<p>{$e3->getName()}</p>";
//echo "<p>$e1->name</p>";
if($e2 instanceof Employees){
  echo "<p>ok</p>";
}else{
  echo "<p>no</p>";
}
unset($e1,$e2,$e3);

Run results:

e1 : 0 : parent : __construct

e2 : 1 : parent : __construct

e2 , 2 : self : __construct

e3 : 1 : parent : __construct

e1 is working

e2 is working

e3 is working

e2 assign jobs

e3 is coding

e3

ok

parent unset e1

self unset e2

parent unset e3

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: