0

Assume the following structure:

<?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;
    }
 }
?>

Here's my client code:

<?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(); 
?>

And the tests:

<?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
?>

As far as I can see, I'm using properties of Inheritance correctly. So why can't I access the parent class properties through my child class?

1
  • your parent::__construct(); makes no sense at the moment Commented Mar 18, 2017 at 20:35

2 Answers 2

1

You're misunderstanding inheritance completely, and confusing class definitions with instances.... each instance you create is unique, and has its own property values; but you don't create separate instances of each class within the inheritance tree, and expect them to access each other.

An instance of the child class inherits the properties and methods of the parent class

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"
Sign up to request clarification or add additional context in comments.

4 Comments

get_parent_start_date() and get_parent_end_date() don't give any benefit as Event_Times class is already inherited superclass getters. Such overridden is a waste
@RomanPerekhrest - I know, but short of writing a full tutorial, I'm trying to emphasise the difference between classes and instances, as a way of explaining inheritance
@RomanPerekhrest - get_parent_start_date() & get_parent_end_date() were merely an illustration to get the point across. The point I was really trying to make was 'how do i get access to the parent properties'?
Great answers, but I have to select one. I feel @MarkBaker appeared to re-enforce OOP stuff somewhat.
1

you need to specify the $start_date and $construct_date when you construct the Event_Times too, otherwise it will call that method with the default values, which are null in you case

try

    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;
    }
 }

then your code

    <?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); 
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.