Solved

using classes

Posted on 2003-11-22
7
291 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.
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 …

733 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