最近,我完成了一个web项目,其中php版本为5.3,然后我将其上传到一个服务器中,其中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();
}
}
现在,error msg再次出现。任何解决办法。
发布于 2013-08-24 16:43:56
你的问题不清楚。以下是手册中的摘录:类别();,它将将当前类输入到作为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;
}
}
也许可以修改您的函数以遵循此过程。
https://stackoverflow.com/questions/18424562
复制