Justtechjobs.com Find a programming school near you






Online Campus Both


php-general | 2004032

[PHP] OOP Class Interaction From: Jonathan Pitcher (jpitcher <email protected>)
Date: 03/31/04

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 ?

Thanks in advance,

Jonathan Pitcher