2010-02-11 58 views
0

我想在调用raw时访问$ lastID有没有办法做到这一点?外部世界可访问的函数内的变量

public static $lastID; 
public function raw($sql){ 

    if(!$result = mysql_query($sql)){ 
     throw new Exception("Could not perform query: " .mysql_error()); 
    } 

    self::$lastID = mysql_insert_id(); 
    return($result); 

} 

编辑:它是一个类成员,它是静态的。

回答

2

的情况下并不清楚,但它看起来好像$ lastID是一类memeber,在这种情况下访问它从类的方法中,你应该使用:

$this->lastID; 

其他问题我可以在代码中看到的是,这条线将无法正常工作

self::$lastID = mysql_insert_id(); 

由于$lastID不是静态的。无论是申报$lastID静态

//change this 
var $lastID; 

//to this to declare $lastID as static 
static $lastID; 

或者使用$this->而不是self::

$this->lastID = mysql_insert_id(); 
+0

好的,我宣布它是静态的,但我仍然感到困惑,因为我如何在课堂外访问它。 – kylex 2010-02-11 15:36:42

+0

NM,算出来: CLASSNAME :: $ lastID; – kylex 2010-02-11 15:40:31

1

你不得不在屋里生

global $lastId; 

使用这里的相关文件http://ca.php.net/manual/en/language.variables.scope.php

+0

使用您不能声明变量(在这种情况下,它的状态将被跨越类的所有对象共享)'var'当它不是一个班级成员时。除非是方法,否则您也不能将函数声明为public。 – Yacoby 2010-02-11 15:34:30

相关问题