[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

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!!!
0
jappievw
Asked:
jappievw
  • 3
1 Solution
 
LornaJaneCommented:
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 :)
0
 
shmertCommented:
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...
0
 
shmertCommented:
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)
0
 
jappievwAuthor Commented:
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?
0
 
shmertCommented:
serializing and deserializing an object results in a copy of the original object, not a reference.  Using a session to store an object is going to serialize it also, even if you don't do so explicitly.

You might need to thing of some other method for getting the related objects, such as storing them in the global namespace, and having your child objects get a reference to this global object instead of storing it as a class property.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now