PHP继承 - 访问父属性

时间:2017-03-18 20:29:11

标签: php

假设以下结构:

<?php
 class Event_Dates {

    public $start_date;

    public $end_date;   

    public function __construct( $start = null, $end = null ){
        $this->start_date = $start;
        $this->end_date = $end;
    }

    public function get_start_date(){
        return $this->start_date;
    }

    public function get_end_date(){
        return $this->end_date;
    }
}


class Event_Times extends Event_Dates
{
    public function __construct()
    {
        parent::__construct();
    }

    public function get_parent_start_date(){
        return $this->start_date;
    }

    public function get_parent_end_date(){
        return $this->end_date;
    }
 }
?>

这是我的客户代码:

<?php
 $start = "2017-03-12 04:00:00";
 $end = "2017-03-12 17:00:00";
 $event_dates = new Event_Dates( $start, $end );
 $event_times = new Event_Times(); 
?>

测试:

<?php 
 var_dump( $event_dates->get_start_date() ); // string(19) "2017-03-12 04:00:00" 
 var_dump( $event_dates->get_end_date() ); // string(19) "2017-03-12 17:00:00"
 var_dump( $event_times->get_parent_start_date() ); // NULL
 var_dump( $event_times->get_parent_end_date() ); // NULL
?>

据我所见,我正确使用了继承属性。那么为什么我不能通过我的子类访问父类属性?

2 个答案:

答案 0 :(得分:1)

你完全误解了继承,并且将类定义与实例混淆....你创建的每个实例都是唯一的,并且有自己的属性值;但是你不要在继承树中创建每个类的单独实例,并期望它们相互访问。

子类的实例继承父类的属性和方法

class Event_Dates {

    public $start_date;

    public $end_date;   

    public function __construct( $start = null, $end = null ){
        $this->start_date = $start;
        $this->end_date = $end;
    }

    public function get_start_date(){
        return $this->start_date;
    }

    public function get_end_date(){
        return $this->end_date;
    }
}


class Event_Times extends Event_Dates
{
    public function __construct( $start = null, $end = null )
    {
        parent::__construct( $start, $end );
    }

    public function get_parent_start_date(){
        return $this->start_date;
    }

    public function get_parent_end_date(){
        return $this->end_date;
    }
 }

$start = "2017-03-12 04:00:00";
$end = "2017-03-12 17:00:00";
$events = new Event_Times( $start, $end );


 var_dump( $events->get_start_date() ); // string(19) "2017-03-12 04:00:00" 
 var_dump( $events->get_end_date() ); // string(19) "2017-03-12 17:00:00"
 var_dump( $events->get_parent_start_date() ); string(19) "2017-03-12 04:00:00" 
 var_dump( $events->get_parent_end_date() ); string(19) "2017-03-12 17:00:00"

答案 1 :(得分:1)

你需要在构造Event_Times时指定$ start_date和$ construct_date,否则它会使用默认值调用该方法,在你的情况下为null

    class Event_Times extends Event_Dates
{
    private $parent;
    public function __construct($parent)
    {
        $this->start_date = $parent->start_date;
        $this->end_date = $parent->end_date;
        }

    public function get_parent_start_date(){
        return $this->start_date;
    }

    public function get_parent_end_date(){
        return $this->end_date;
    }
 }

然后你的代码

    <?php
 $start = "2017-03-12 04:00:00";
 $end = "2017-03-12 17:00:00";
 $event_dates = new Event_Dates( $start, $end );
 $event_times = new Event_Times($event_dates); 
?>