Hi,

I was wondering whether it is possible to use a parent class' properties without calling its constructor. In the following example:

<?php
class Shop {    
    public $location;   
    public function __construct($location = ''){       
        if(!empty($location)){
            $this->location = $location;
        } else {
            $this->location = 'a place far far away';
        }
        $this->location .= '. ';        
        echo 'Shop constructor has been called. ---- ';        
    }    
    public function check_stock($item){        
        echo 'There are still quite a few '. $item.'s remaining.';        
    }    
}

class Item extends Shop {    
    //public function __construct(){}   
    public function find($item){        
        echo $item . ' can be found at '.$this->location;
        $this->check_stock('Bed');       
    }    
}

$shop = new Shop('Carreyville');
$item = new Item;
$item->find('Bed');

// output: Shop constructor has been called. ---- Shop constructor has been called. ---- Bed can be found at a place far far away. There are still quite a few Beds remaining.

?>

In this script, it can be seen that the constructor has been called twice, since the child is called the parent construct again after the parent had already been instantiated. However, if the child doesn't call the parent constructor, the parent's properties cannot be accessed.

<?php 
class Item extends Shop {    
    public function __construct(){}   
    public function find($item){        
        echo $item . ' can be found at '.$this->location;
        $this->check_stock('Bed');       
    }    
}

// output: Shop constructor has been called. ---- Bed can be found at There are still quite a few Beds remaining.
?>

Is there anyway to access the parent properties without having to call the parent's constructor again since it has already been instantiated before the child.

Thanks.

Recommended Answers

All 2 Replies

Do you understand the purpose of object inheritance?

Simply, the idea is that when two objects have similar attributes and methods, rather than declare the attributes and methods twice, you can use inheritance - DRY - Don't Repeat Yourself.

From the example you have provided, I cannot see why an Item object would extend a Shop object. Instead, an Item should probably belong to a Shop.

Anyway, I'm not going to get into debating the architecture of your application. That is something you can consider yourself.


Usually when using inheritance, if the child class had it's own constructor, you would call the parent constructor from within the child constructor. E.g.

class Item extends Shop {
    protected $_itemId;

    public function __construct($itemId = 0, $location = '') {
        $this->_itemId = $itemId;

        parent::__construct($location);
    }
}

If you don't want to do that, then another option would be to implement the decorator design pattern.

R.

Do you understand the purpose of object inheritance?
Usually when using inheritance, if the child class had it's own constructor, you would call the parent constructor from within the child constructor.

I guess I don't. I saw a child class as an extension of the parent class such that the child would just provide extra functionality. I guess that isn't what I needed in my application (not the one in the example).

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.