我努力學習OOP,我想從一個一個函數值傳遞給另一個類中,但由於某種原因,它給我的Notice: Trying to get property of non-object
錯誤,任何想法?PHP傳球類值從一個功能到另一個
class test{
function test($value){
global $db;
$stmt = $db->prepare("SELECT * FROM some_table where some_column = ?");
$stmt->bind_param('s', $value);
$stmt->execute();
$res = $stmt->get_result();
$fetch = $res->fetch_object();
$this->test = $fetch->some_row;//this is the error line
}
function do_something(){
$name = $this->test;
return $name;
}
}
$p = new test();
$p->test('test');
echo $p->do_something();
它還指出你在那裏發生的確切行。由於您知道行號和變量,因此請使用'var_dump()'並檢查** actual **變量值。 – zerkms 2014-09-27 03:27:53
@zerkms當我做var_dump時,它返回NULL。 – user2666310 2014-09-27 03:48:22
所以 - 這就是爲什麼你會得到一個錯誤。你試圖調用一個NULL的方法,這是無稽之談。 – zerkms 2014-09-27 03:49:47