I'm trying to get my design straight and am wondering if there is a way to "attach" a child-class to an already instantiated parent-class.
Example:
CLASS webshopOrder { ... }
CLASS deliveryAddress EXTENDS webshopOrder {
public function __construct($parentOrder) {
// pseudo: parent = $parentOrder;
}
}
$order = new webshopOrder;
$deliveryAddress = new deliveryAddress($order);
Now, when I need the delivery address later in my code I don't want the order to initiate (expensively) again and would like to use the existing instance. Any hint would be appreciated.
I think you can inject address class in your order class by constructor, and there is little mistake in your design, logically shop order should contain the delivery address, not address have order. so I make some update:
class DeliveryAddress{..}
class WebShopOrder {
public $deliveryAddress;
public function __construct(DeliveryAddress $deliveryAddress) {
$this->deliveryAddress = $deliveryAddress;
}
}
$delivery_address = new DeliveryAddress;
$order = new WebShopOrder($delivery_address);