Jump to content

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>');
   }
}

?>

Edited by 3raser
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.