Solved

using classes

Posted on 2003-11-22
7
279 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
  • 3
7 Comments
 
LVL 5

Expert Comment

by:LornaJane
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now