php 扩展引入

继承引入参数

<?php


class A
{
    private $name;
    private $age;
    private $time;

    public function __construct($name, $age)
    {
        $this->name = $name;
        $this->age = $age;
        $this->time=date("Ymd");
    }
    /**
     * @return mixed
     */
    public function hello()
    {
        echo $this->time;
    }
}

class B extends A
{
    private static $name = '123';
    private static $age = 12;

    public function __construct()
    {
        parent::__construct(self::$name, self::$age);
    }
}
$obj = new B();
echo $obj->hello();
View Code

实例化引入重写

<?php


class A
{
    private $name;
    private $age;
    public $time;

    public function __construct($name, $age)
    {
        $this->name = $name;
        $this->age = $age;
        $this->time=date("Ymd");
    }
    /**
     * @return mixed
     */
    public function hello()
    {
        echo $this->time;
    }
}

class B
{
    public static $a;
    public function __construct()
    {
        $name='jack';
        $age=23;
        self::$a=new A($name,$age);
    }

    public function test(){
        echo self::$a->time;
    }
}
$obj = new B();
$obj->test();
View Code
原文地址:https://www.cnblogs.com/huay/p/11047254.html