我是OOP的新手,我有两节课。
class db{
private $_database;
public function __construct(){
try{
$this->_database = new PDO('mysql:host='.'localhost'.';dbname='.'lala','root','');
}catch(PDOExeption $e){
die($e->getMessage());
}
}
public function query(){
$query = $this->_database->query('SELECT * FROM users');
$query->setFetchMode(PDO::FETCH_OBJ);
$query->execute();
$row = $query->fetchAll();
return $row;
}
}
第一类是数据库。
class users{
public $id, $name, $password, $mail;
private $_databaseUSERS, $_result;
public function __construct(){
$this->_databaseUSERS = new db();
}
public function nameAndpass(){
echo "Name: {$this->name}, Password: {$this->password}".'<br>'.'<br>'.'<br>'.'<br>';
}
public function get(){
$this->_result = $this->_databaseUSERS->query();
$object_array = array();
foreach ($this->_result as $key => $value) {
$object_array[] = self::blabla($value);
}
return $object_array;
}
public function blabla($record){
foreach($record as $attribute => $value){
$this->$attribute = $value;
}
return $record;
}
}
,第二类是用户。
$users = new users();
$users->get();
echo $users->nameAndpass();
echo $users->mail.'<br>'
这是工作和输出是:
姓名:Johny,密码:123456
Johny@mail.com
$users = new users();
$user = $users->get();
foreach ($user as $userss) {
echo $userss->name.'<br>';
}
这也是有效的,输出是:
杰克
比利
约翰尼拉
$users = new users();
$user = $users->get();
foreach ($user as $userss) {
echo $userss->nameAndpass().'<br>';
}
这不起作用,输出是:致命错误:调用未定义 方法stdClass :: nameAndpass()in C:\ Users \用户伊拉克利梅\桌面\工作\ XAMP \ htdocs中\的index.php
如何解决此错误?