Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP OOP未定义>;方法stdClass_Php_Mysql_Database_Oop - Fatal编程技术网

PHP OOP未定义>;方法stdClass

PHP OOP未定义>;方法stdClass,php,mysql,database,oop,Php,Mysql,Database,Oop,我是OOP新手,我有两门课 class db{ private $_database; public function __construct(){ try{ $this->_database = new PDO('mysql:host='.'localhost'.';dbname='.'lala','root',''); }catch(PDOExeption $e){ die($e->

我是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;
    }
}
类用户{
public$id、$name、$password、$mail;
私有$\u数据库用户,$\u结果;
公共函数构造(){
$this->_databaseUSERS=new db();
}
公共函数nameAndpass(){
echo“名称:{$this->Name},密码:{$this->Password}”。




; } 公共函数get(){ $this->_result=$this->_databaseUSERS->query(); $object_array=array(); foreach($this->\结果为$key=>$value){ $object_array[]=self::blabla($value); } 返回$object\u数组; } 公共功能布拉布拉($record){ foreach($记录为$属性=>$值){ $this->$attribute=$value; } 返回$record; } }
第二类是用户

$users = new users();
$users->get();
echo $users->nameAndpass();
echo $users->mail.'<br>'
$users=新用户();
$users->get();
echo$users->nameAndpass();
echo$users->mail.“
这正在工作,输出为:

姓名:Johny,密码:123456

Johny@mail.com

$users=新用户();
$user=$users->get();
foreach($useras$userss){
echo$userss->name.“
”; }
这也是可行的,输出为:

杰克

比利

约翰尼

$users=新用户();
$user=$users->get();
foreach($useras$userss){
echo$userss->nameAndpass()。
; }
这不起作用,输出为:致命错误:调用未定义 中的方法stdClass::nameAndpass() C:\Users\Irakli\Desktop\Work\xamp\htdocs\index.php


如何修复此错误?

不是name\u pass()吗?只是键入错误。。。我编辑过。但问题是一样的。看起来你刚刚编辑了这个问题……现在它就在那里……坦率地说:你认为你的
blabla
函数在做什么?它实际上没有做任何事情,或者至少没有做任何明智的事情;返回一个对象数组,并使用您试图访问用户成员函数的数据。
$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>';
}