Home  >  Article  >  Backend Development  >  Son inherits father's business, php inherits private goods

Son inherits father's business, php inherits private goods

autoload
autoloadOriginal
2021-03-03 11:08:542802browse

Definition: Limited inheritance means that when subclass inherits members of parent class, it does not inherit everything, but inherits and uses the parent class Part. The popular understanding is that when the company's previous CEO left office, he planned to hand over some things in the company to the current CEO. The documents and work content of the handover can be recorded in writing and reviewed at any time, but for some work insights, these are If there is no way to hand over, this is the limited inheritance of the current CEO from the previous CEO.

1. Inheritance content: Inheritance in PHP means that the subclass inherits all the public members , protected members and of the parent class. Private attributes cannot inherit the private methods of the parent class.

<?php
    class A{
      
        public $name=&#39;张三&#39;;
        protected $sex=&#39;man&#39;;
        private  $age=&#39;25&#39;;

        public function getName(){
           echo $this->name."<br>";
        }
        protected function getSex(){
           echo $this->sex."<br>";
        }
        private function getAge(){
          echo $this->age."<br>";
        }

    }
    class B extends A{
        
        public function getA(){
           $this->getName();
           $this->getSex();
           $this->getAge();//父类私有方法无法继承

        }
    }

    $a=new B();
    var_dump($a);
  /*object(B)#1 (3) { 
                          ["name"]=> string(6) "张三" 
                          ["sex":protected]=> string(3) "man" 
                          ["age":"A":private]=> string(2) "25" }
  */
?>

2.Protected inheritance protected

The generation of the protected keyword is purely for inheritance. It means that the subclass is allowed to be accessed within the subclass, but is not allowed to be directly accessed from the outside.

<?php
    class A{
       public $name=&#39;张三&#39;;
        protected $sex=&#39;man&#39;;
        private  $age=&#39;25&#39;;

        public function getName(){
           echo $this->name."<br>";
        }
        protected function getSex(){
           echo $this->sex."<br>";
        }
        private function getAge(){
          echo $this->age."<br>";
        }

    }
    class B extends A{
        public function getA(){
           $this->getName();
           $this->getSex();
      }
    }
    $a=new B();
    var_dump($a);
    echo "<br>";
    
   //$a->getSex();    //: Call to protected method A::getSex() from context  不能在外部访问
  
?>

Tips: Access private members of the parent class: If a subclass wants to access private members of the parent class, the premise is that the parent class provides public or protected methods for the subclass to access (generally not recommended, otherwise set The meaning of private is gone),.

3. Static members (class constants)

also follow the inheritance rules (PHP inheritance is essentially an object), but the access method is by class.

<?php
class Fu{
    const NAME = &#39;人&#39;;
    public static $count = 0;
    protected static $type = array(&#39;黑&#39;,&#39;黄&#39;,&#39;白&#39;);
    
    public static function getCount(){
        echo self::NAME;
        echo self::$count;
    }
    protected static function getType(){
        print_r(self::$type);
    }
}

class Zi extends Fu{
    //依然需要子类中访问受保护成员
    public static function getFu(){
        Fu::getType();
    }
}

echo Zi::$count;	//允许直接访问
Zi::getCount();	//访问父类静态方法
Zi::getFu();	//利用子类公有方法访问父类受保护成员
?>

4.Construction method and Destruction method can also be inherited by subclasses. At this time, you need to pay attention to the corresponding when instantiating subclass objects. The parameters of the parent class constructor method.

<?php
//父类
class Fu{
    private $money;
    public function __construct($money){
        $this->money = $money;
    }
    
    public function __destruct(){
        echo &#39;die&#39;;
    }
}

//子类继承
class Zi extends Fu{}

//子类实例化:自身是空类,没有指定构造方法
//$m = new Zi();					//错误:缺少参数,因为会自动调用父类构造方法
$m = new Zi(100);					//正确
?>

Recommended: php tutorial

The above is the detailed content of Son inherits father's business, php inherits private goods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn