pocobueno1388 Posted March 24, 2009 Share Posted March 24, 2009 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 Link to comment https://forums.phpfreaks.com/topic/150966-solved-oop-using-class-within-another-class/ Share on other sites More sharing options...
blueman378 Posted March 24, 2009 Share Posted March 24, 2009 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 Link to comment https://forums.phpfreaks.com/topic/150966-solved-oop-using-class-within-another-class/#findComment-793174 Share on other sites More sharing options...
trq Posted March 25, 2009 Share Posted March 25, 2009 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. Link to comment https://forums.phpfreaks.com/topic/150966-solved-oop-using-class-within-another-class/#findComment-793211 Share on other sites More sharing options...
pocobueno1388 Posted March 25, 2009 Author Share Posted March 25, 2009 Thanks thorpe, it was because the methods were static. Link to comment https://forums.phpfreaks.com/topic/150966-solved-oop-using-class-within-another-class/#findComment-793245 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.