Charles Kirk Charles Kirk - 6 months ago 18
PHP Question

OOP Inheritance

another developer and I are in the process of building a site, we implemented our own OOP model which we had a lot of discussion on, but it seems I've run into a brick wall. Please bear with me

All our classes extend Object, which has a constructor, which uses

get_called_class()
to get the class, and then get_class_vars() etc to load column names, which means we can have a
save
,
delete
,
displayExisting
,
getAll
, and a few other methods in a class by default, which eliminates a lot of code. This is good. Now, I have a class
InboundData
, this has 3 classes extend off of itself, called
Visit
,
Enquiry
, and
BrochureRequest
, they have an "inbound_data_id," then InboundData has an overloaded constructor looking something like this:

function __construct($id = '')
{
$this->className = get_called_class();
$this->id = (int)$id;
$this->tableName = strtolower($this->className);
$this->setDbFields();
$this->loadData();

$this->contact = new Contact($this->contact_id);
$this->address = new Address($this->address_id);
$this->domain = new Domain($this->domain_id);
$this->park = new Park($this->park_id);
}


The problem is, that
contact
,
address
,
domain
, and
park
are all protected, as to stop Object's method's from picking them up as column names/class vars. This is the methodology we have adopted on all non-db members, such as:

protected $contact;
protected $address;
protected $domain;
protected $park;


We haven't run into any problems, until writing the frontend code, I found out that I obviously can't fill in
$formObj->contact->var
etc, not to mention I haven't wrote any save code for the inbound data extended objects, which will need to save all the attached tables.

Please could an OOP guru let us know where we've gone wrong, and what the best way to accomplish this would be. Feel free to ask any questions, I know this must sound rather confusing.

Answer

I'm not sure if I understand what you are trying to do, but it sounds like you have a Template object of some kind that displays the page, and you want to get the Enquiry (for instance) contact value and apply it to the template. I suggest doing something like this:

class TemplateBuilder {
    public static function addDataToTemplate(Template $tpl, Object $enq) {
       $enq->addToTemplate($tpl);
    }
}

This is not totally necessary, but the next step is:

class Enquiry extends Object {
   ...
   public function addToTemplate(Template $tpl) {
      $tpl->contact->var = $this->contact;
   }
}

This keeps the Enquiry-only data protected just like you want it to be, and you get very specific control over what data you want to add to which objects.

This is a bit off topic, but I'd suggest not using extension and going through all of this magic. If you have a lot of utility methods that the classes extending Object can use, you can construct them with Object (or whatever its called) as a member instead and use it via composition.

Comments