Justtechjobs.com Find a programming school near you






Online Campus Both


php-general | 2004032

Re: [PHP] OOP Class Interaction From: Robert Cummings (robert <email protected>)
Date: 03/31/04

On Wed, 2004-03-31 at 12:55, Jonathan Pitcher wrote:
> I have been writing OOP programs for the past couple years. Over these
> years I have run into the same problem, I have solved the problem in
> many different ways. I was wondering if there is a recommended way to
> solve the problem I have listed below.
>
> To keep it simple, lets say I have 3 classes. A main class, an error
> class and a log class.
>
> The classes are laid out as:
>
> MAIN
> |
> ----------------
> | |
> ERROR LOG
>
>
> Now I want error to write a message to the log file.
>
> Solution 1
>
> Store the Main class in a Session variable. And the access the
> Log through main.
>
> $_SESSION["Main"]->Log->Write_Error("My Error");
>
> Solution 2
>
> Almost the same as above. I store main in a Session Variable but
> the I create a global function to access log.
>
> function Write_Error($Message)
> {
> $_SESSION["Main"]->Log->Write_Error($Message);
> }
>
> This ways saves coding time because I don't have to write out the long
> session reference.
>
>
> I know there are more ways to do this. But every way I can think of
> requires you to store the main class in a session variable to access
> log. Is there a way to access a parent class or a parents parent
> without doing what I did above ?

You can use a base object service class. The InterJinn framework uses
this method to access all such classes. In this way a single common and
inheritable method can be used to retrieve singleton objects, or to act
as a factory. This provides loose coupling for any given object from the
consumer of it's services. For instance:

class Exception extends BaseClass
{
    function triggerError( $errorMsg )
    {
        // Some erroneous condition.

        $log = &$this->getServiceRef( 'log' );
        $log->log( $errorMsg );
    }
}

InterJinn does a lot of things behind the scenes in the getService()
method such as lazy loading of the source code, and lazy instantiation
of the object. This way unused services have a minimal impact on
application's performance when they are not actually needed. Hope this
helps you with your question.

Cheers,
Rob.

-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php