Link to home
Start Free TrialLog in
Avatar of jappievw
jappievw

asked on

using classes

I'm making a website with a very large set of classes. I only want to initialize 1 class in the main script. That class, called BaseSystem, initializes the other classes. Sometimes I need to get information from the parent-class. But I really don't know how to substract this information. Currently I'm doing it this way:

class BaseSystem
{
      var $this_settings;
      var $requesturl;
      var $currentpage;
      var $database;
      var $error;
      
      function BaseSystem($in_array)
      {
            $this->this_settings['uid'] = "_guest";
            $this->this_settings['sid'] = session_id();
            $this->this_settings['pid'] = "_home";
            $this->this_settings['database_admin_email'] = $in_array[6];
            
            $this->requesturl = new RequestUrl(& $this, $in_array[4]);
            $this->database = new Database(& $this, array($in_array[0], $in_array[1], $in_array[2], $in_array[3]));
            $this->error = new Error(& $this);
            
            $this->StartPage($this->GetVar("pid"));
      }
      function GetVar($var)
      {
            return $this->this_settings[$var];
      }

}

The classes RequestUrl and Database currently don't require information from the parent class (BaseSystem). The Error class does need information.

class Error
{
      var $parent;
      
      function Error($par)
      {
            $this->parent = $par;
      }
      
      function LogError($text)
      {
            
      }
      
      function GenerateError($text, $pid)
      {
            $errorpid = $this->parent->GetVar("pid");
            
            $this->parent->requesturl->SetArgVar("errortext", $text);
            $this->parent->requesturl->SetArgVar("errorpid", $errorpid);
            header("Location: " . $this->parent->requesturl->GetUrl("_error") . " \r\n");
            exit;
      }
}

My problem is, that I want a hierarchy of classes, which can interact with eachother and which is saved in a session.  Currently I don't get the correct information from the $this->parent var. I get the information from the moment, the error-class was instantiated. How can I use a hierarchy of classes?

Thanks in advance!!!
Avatar of LornaJane
LornaJane

I'm a bit hazy on this one but you could try replacing $this->parent with parent::, For example

parent::requesturl->SetArgVar("errortext", $text);

I hope this helps :)
You need to use more reference pointers.  Your parent object is being copied in the error constructor, do something like this:

class Error
{
     var $parent;
     
     function Error(&$par)
     {
          $this->parent =& $par;
     }


Note that there are 2 new "&" operators.  It's really tough to figure out when to use these and when not to.  Always use them when assigning an object to a variable, or when an object is an argument to a function.  Don't use them if the argument might not be an object at some point, since some plain values will generate an error when you try to pass a reference to them.  Also, incidentally, you should avoid the syntax you use in your base class,
    $this->error = new Error(& $this);
because putting the "&" in the calling code has been deprecated, and will generate an error in newer v. of PHP.

This is a big pain to implement everywhere, since it's so easy to accidentally omit one of the "&" reference operators, and there's no immediate feedback when you do forget.  Your other option is to upgrade to php5, which always passes objects as references instead of as copies.  Unfortunately it's still beta...
p.s. you should also use references in the base class when you're instantiating all these suckers:

change this:
    $this->error = new Error(& $this);
to this:
    $this->error =& new Error($this);

since you're assigning the newly created Error object to the $this->error variable (see above post)
Avatar of jappievw

ASKER

Thanks for your comments. However it isn't working in the way I thought it would go working. I made de references the way you (shmert) advised me to do. Maybe I know the source of the problem. I save the object of the BaseSystem class in a sessionvariable. When the same visitor goes to anonther page, the same object is used. Maybe it's the problem that the references have been changed? How can I solve this problem then? Is there a need to use serealising?
ASKER CERTIFIED SOLUTION
Avatar of shmert
shmert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial