Charles Kirk
Charles Kirk

Reputation: 167

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.

Upvotes: 1

Views: 256

Answers (2)

Explosion Pills
Explosion Pills

Reputation: 191749

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.

Upvotes: 1

Louis-Philippe Huberdeau
Louis-Philippe Huberdeau

Reputation: 5431

Generally extending everything from Object is not such a great idea. It adds plenty of methods in your model that do not really belong. It also breaks basic is-a relations you would usually expect from a good object model.

Building the back-end without considering the front-end was also not such a good idea. It leads to wasted efforts.

As far as setting properties go. Can't you simply add getters/setters to access what you need? Have you considered using __get() and __set() magic methods?

A common way to keep track of modifications that need to be saved is to use an isDirty flag. The next issue to keep track of objects you loaded from the database. This is usually resolved by using an entity manager.

If you run into issues setting properties on objects that can't be resolved, you can use the Reflection API to force setting some properties as well. ReflectionProperty::setAccessible() can allow you to set a value on an private/protected property.

Upvotes: 1

Related Questions