Charles Kirk Charles Kirk - 2 years ago 99
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

to get the class, and then get_class_vars() etc to load column names, which means we can have a
, and a few other methods in a class by default, which eliminates a lot of code. This is good. Now, I have a class
, this has 3 classes extend off of itself, called
, and
, 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->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
, and
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
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 Source

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) {

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download