2013-08-25 58 views
0

最近我已经完成了一个web项目,其中php版本是5.3,然后我上传到服务器,其中php版本是5.2。*。然后显示以下信息:get_called_class in php 5.2 *

Fatal error: Class name must be a valid object or a string in /home/user_folder/public_html/_includes/class.myclass.php on line 264 

在我class.myclass.php我用下面的代码::

protected function get_table_value($record) { 
    $className = get_called_class(); // 

    $object = new $className; // line 264 
    // rest of the code 
} 

然后我改变了代码如下::

// get the database table's column's value 
    protected function get_table_value($record) { 
     $className = $this->generate_class_name(); 
     $object = new $className; // same error in this line 

     // rest of code 
    } 

    private function generate_class_name() { 

     if (!function_exists('get_called_class')) { 
      $bt = debug_backtrace(); 
      $l = count($bt) - 1; 
      $matches = array(); 
      while(empty($matches) && $l > -1){ 
       $lines = file($bt[$l]['file']); 
       $callerLine = $lines[$bt[$l]['line']-1]; 
       preg_match('/([a-zA-Z0-9\_]+)::'.$bt[$l--]['function'].'/', 
       $callerLine, 
       $matches); 
      } 
      if (!isset($matches[1])) $matches[1]=NULL; //for notices 
       if ($matches[1] == 'self') { 
       $line = $bt[$l]['line']-1; 
       while ($line > 0 && strpos($lines[$line], 'class') === false) { 
       $line--; 
      } 
      preg_match('/class[\s]+(.+?)[\s]+/si', $lines[$line], $matches); 
      } 
     return $matches[1];    
     } 
     else {     
      return get_called_class(); 
     }    
    } 

现在再次说明错误消息来了。任何解决方案

回答

0

你的问题还不清楚。以下是本手册的摘录:Get_Class(); 这些都将输入当前类为return语句作为一个VAR(见fig2):

图1

class foo { 
    function name() 
    { 
     echo "My name is " , get_class($this) , "\n"; 
    } 
} 

// create an object 
$bar = new foo(); 

// external call 
echo "Its name is " , get_class($bar) , "\n"; 

// internal call 
$bar->name(); 

图2

class foo{ 
    public function Test(){ 
    $Classname = get_class($this); 
    $Instance = new $Classname; 
    return $Classname; 
    } 
} 

也许修改您的功能以遵循此过程。

+0

它的工作原理。很多很多谢了 – sabbir