Forum Moderators: coopster

Message Too Old, No Replies

What inheritance approach?

Confused by this situation.

         

TomAnthony

11:38 pm on May 7, 2006 (gmt 0)

10+ Year Member



I have decided I should start building myself a set of libraries for my upcoming projects.

My experience with OO design is limited but I do get it, but previously I used C++ which supported multiple inheritance, unlike PHP.

Now I've started working with PHP5 (stuck with 4 before now), so decided to build up a set of classes.

However, I am confused by this situation; there may not be a 'right' way to do this, but I'd really appreciate the thoughts of those with more experience.

I have a class 'abLog' which can write log entries to the current page, to a text file, or a database (you specify which when you create the object).

A couple of my other classes, including 'abForm' have the following functions: set_logging(), log_error(), log_activity().

set_logging sets the current logging level (do you wish to log errors, activity, or both) and also takes an 'abLog' object.

log_error() and log_activity() check if their respective flag is set, and if so they write out to the 'abLog' object.

The set_logging(), log_error(), log_activity() are going to be the same for all classes, so I planned to write an 'abLogger' class that they each inherit.

Is this approach 'correct'? Is there a better way to go about this? I may want my classes to inherit from other objects in the future, so would this be limiting myself (I guess I could do abLogger->abObject->abOtherObject, so this wouldn't limit me).

Any feedback would be much appreciated.

I've included the functions below, just to further clarify if I wasn't clear.


public function set_logging($log_flags, $log_object)
{
if (($log_flags & FORM_LOG_ERRORS) == FORM_LOG_ERRORS)
{
$this->log_errors = true;
$this->log_errors_target = $log_object;
}else{
$this->log_errors = false;
}

if (($log_flags & FORM_LOG_ACTIVITY) == FORM_LOG_ACTIVITY)
{
$this->log_activity = true;
$this->log_activity_target = $log_object;
}else{
$this->log_activity = false;
}
}

private function log_error($log_entry)
{
if ($this->log_errors)
$this->log_errors_target->write($log_entry);
}

private function log_activity($log_entry)
{
if ($this->log_activity)
$this->log_activity_target->write($log_entry);
}

coopster

10:04 pm on May 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I believe I understand what you are getting at. Do you NOT want to use extends? On a side note, you could have a look at how the PEAR classes extend each other to get some ideas on how you might implement your design.

TomAnthony

1:26 pm on May 14, 2006 (gmt 0)

10+ Year Member



What I ended up doing was having ab 'abObject' class, that all my classes extend, and the logging functions I want all my classes to have are in place.

Unfortunately, a couple of my classes can't inherit 'abObject' as they extend PHP's built in classes, but there doesn't seem to be much that can be done about that.

I did see articles on runkit_class_adopt, but that looks messy.