Solved

using classes

Posted on 2003-11-22
7
294 Views
Last Modified: 2008-02-26
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
Comment
Question by:jappievw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
7 Comments
 
LVL 5

Expert Comment

by:LornaJane
ID: 9804063
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
 
LVL 11

Expert Comment

by:shmert
ID: 9805187
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
 
LVL 11

Expert Comment

by:shmert
ID: 9805188
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
 

Author Comment

by:jappievw
ID: 9805497
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
 
LVL 11

Accepted Solution

by:
shmert earned 125 total points
ID: 9806257
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question