我得到:未定義私有變量
未定義的變量:密碼
未定義的變量:主機
未定義的變量:用戶
我只是好奇,爲什麼我收到這樣的通知, 雖然該變量已在類的專用部分中定義。
我不能在成員函數中使用該類的私有數據成員(因爲這會打敗OOP的整個概念)?
PHP文件是:
class data_base //helps handling permissins
{
private $host;
private $user;
private $password;
public function feed_data($hst, $usr, $pwd)
{
$host=$hst;
$user=$usr;
$password=$pwd;
}
public function get_data()
{
$info=array("host"=>" ", "user"=>" ", "password"=>" ");
$info['host']=$host;
$info['user']=$user;
$info['password']=$password;
return $info;
}
}
$user1=new data_base;
$user2=new data_base;
$user1->feed_data("localhost", "root", ""); //enter details for user 1 here
$user2->feed_data("", "", ""); //enter details for user 2 here
$perm_add=$user1->get_data();
$perm_view=$user2->get_data();
在這種特殊情況下,即使只是:'return get_object_vars($ this);'。 – hakre 2012-04-04 07:05:38
@hakre是的。但是我對自己不喜歡,因爲它可能意外暴露超過想要的,當班級延長後。我喜歡更詳細的方式:) – KingCrunch 2012-04-04 07:23:19