Jump to content

Access To Undeclared Static Property


3raser

Recommended Posts

The below function worked just fine when it was in a non-static format. However, whenever I put it into static format and called the function this error was generated:

 

Fatal error: Access to undeclared static property: server::$class in C:\wamp\www\rsplaypages\structure\server.php on line 11

 

Any idea whay self::$class isn't working?

 


public static function loadClass($class){
       if(self::$class == null){
           if(file_exists($class.'.php')){
               include($class.'.php');
               self::$class = new $class();
           }else{
               self::throwError('class '. $class.' does not exist');
           }
       }else{
           self::throwError($class. 'already loaded.');
       }
   }

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/269547-access-to-undeclared-static-property/
Share on other sites

Sorry about that

 


<?php

class server{
   protected $database;

   function __construct(){
       self::$database = loadClass($database);
   }

   public static function loadClass($class){
       if(self::$class == null){
           if(file_exists($class.'.php')){
               include($class.'.php');
               self::$class = new $class();
           }else{
               self::throwError('class '. $class.' does not exist');
           }
       }else{
           self::throwError($class. 'already loaded.');
       }
   }

   public static function dropClass($class){
       self::$class = null;
   }

   public static function runMethod($class, $method, array $params = array()){
       if(self::isLoaded($class)){
           if(count($params) != 0){
               $par = '';
               $i = 1;
               foreach($params as $key => $value){
                   $par .= ($i == count($params)) ? $method : $method.',';
                   $i++;
               }
           }
           self::$class->$method(($par != null) ? $par : null);
       }else{
           self::throwError($class .' is not loaded.');
       }
   }

   protected static function isLoaded($class){
       return (self::$class == null) ? false : true;
   }

   protected static function throwError($error){
       die('<link rel="stylesheet" href="css/error.css" media="all" /><div class="error"><img src="img/error.png" width="45" height="40"><br/><h4>Error!</h4><div class="err_msg">'. $error .'</div></div>');
   }
}

?>

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.