Jump to content

[SOLVED] OOP - Using class within another class


pocobueno1388

Recommended Posts

I am trying to figure out how to use an error class I created within all of the classes I create for my site. Right now I am trying to implement it into a user registration class and am getting an error and can't figure out why.

 

Here is the relevant code from my registration class

 

<?php

class authorize {
   
   .
   .
   .
   public $error;
   
   function __construct($username, $password, $vpassword, $email){
      
      .
      .
      .
      $this->error = new error; //Here I try to embed my error object into the class
      
   }

   function register(){

      //This is how I'm trying to use my error object
      if (strlen($this->username) < 1) $this->error->addError("You must enter a username");
     .
     .
     .

    }

 

The error I'm getting is

Fatal error: Using $this when not in object context in C:\wamp\www\horse\classes\error.class.php on line 9

 

Here is the error classes code

 

<?php

class error {
   
      public $errors = array();
      
      static function addError($error){
         
         $this->errors[] = $error; //This is line 9
         
      }
      
      static function displayErrors(){
         
         $output = "<b>The following errors occured:</b><br><ul>";
         
         foreach ($this->errors AS $error){
            
            $output .= "<li>$error</li>";
            
         }
         
         $output .= "</ul>";
         
         return $output;
         
      }
      
      static function numErrors(){
         
         if (count($this->errors > 0)) return TRUE;
         else return FALSE;
         
      }
   
}

?>

 

I understand that I could just assign the error object to it's own variable inside the class (not as part of the class), but I'm trying to avoid having to make a method to return the errors in every single class.

 

If you have a better approach to this, please share! Thanks

 

hi mate,

I *think* this is correct

 

but if you go class authorize extends error {

//blablabla

}

 

you should be able to access all the methods of your error class,

 

asif it was the same class so in authorize

you might have $this->displayErrors()

 

i think thats right lol

SOMEONE correct me please

Firstly to blueman378. authorize does not extend error, thats like saying you have a car object that extends an orange, they are of no relation.

 

Secondly, the entire error object (in its current form) could likely be done away with in favour of using exceptions.

 

Thirdly, the methods within your error object have been defined as being static, this means they are called within a class context and not that of an instantiated object. ie; $this does not exist within static methods.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.