douqing5981 2013-08-25 00:39
浏览 17

php 5.2中的get_called_class *

Recently I have completed a web project where php version is 5.3, then I upload this into a server where php version is 5.2.*. Then the following message is shown:

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

In my class.myclass.php I use the following code ::

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

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

then I change this code as following ::

// 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();
        }            
    }

And now again that error msg come. any solution.

  • 写回答

1条回答 默认 最新

  • 普通网友 2013-08-25 00:43
    关注

    Your question is unclear. The following is an extract from the manual: Get_Class(); Which will input your current class into a return statement of as a var (see fig2):

    Fig 1

    class foo {
        function name()
        {
            echo "My name is " , get_class($this) , "
    ";
        }
    }
    
    // create an object
    $bar = new foo();
    
    // external call
    echo "Its name is " , get_class($bar) , "
    ";
    
    // internal call
    $bar->name();
    

    Fig 2

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

    Perhaps modify your function to follow this procedure.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100