Php OOP-函数未定义?

Php OOP-函数未定义?,php,oop,Php,Oop,我正在尝试OOP,最近它对我不好。这是我的班级: class db { protected $host = ""; protected $dbname = ""; protected $user = ""; protected $pass = ""; function execute($query, $parameters, $usefetch) { $dsn = "mysql:host=$this->host;dbname=$this->dbname"; $p

我正在尝试OOP,最近它对我不好。这是我的班级:

class db {

protected $host = "";
protected $dbname = "";
protected $user = "";
protected $pass = "";

function execute($query, $parameters, $usefetch) {

    $dsn = "mysql:host=$this->host;dbname=$this->dbname";
    $pdo = new PDO($dsn,$this->user,$this->pass);

    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    try {
        $statement = $pdo->prepare($query);
        $statement->execute($parameters);

        if($usefetch) {
            $result = $statement->fetchAll(PDO::FETCH_ASSOC);
            return $result;
        } else { 
            return SUCCESS; 
        }
    }

catch(PDOException $ex) { return $ex; }

}

function __get($var) {
    return $this->$var;
}

}
当我尝试定义它并调用函数“execute”时,我得到以下错误:

致命错误:未捕获错误:调用中未定义的函数execute() /var/www/html/api/classes/sessions.php:49堆栈跟踪:#0 /var/www/html/index.php(3):抛出require_once()#1{main} /var/www/html/api/classes/sessions.php,第49行

下面是我如何尝试调用它(调试)


我不明白为什么会发生这种错误。这可能与我的php配置有关吗?(它不会执行,即使我让dbexecute执行它自己的函数而不是对象)

发生的事情是
var\u dump()
试图将字符串
$db
execute()的输出连接起来
在全局范围内运行。

您有一个连接
,在这里您应该有
->
。应该是:

$db->execute("SELECT * FROM sessions", [], true)
而不是:

// This is a typo concatenation OR you may be temporarily confused ;) with
// a Javascript object which does use the period for object/method
$db.execute("SELECT * FROM sessions", [], true))

尝试
$db->
而不是
$db.
$db->执行(…yada,yada,yada
-\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuD@Rasclatt在另一个类中,我试图调用$db->execute(),但它说$db未定义;在全局范围内。因此,您需要
类会话{public function}构造(db$db,$token){…等
然后执行
$Session=new Session(new db,$token);
// This is a typo concatenation OR you may be temporarily confused ;) with
// a Javascript object which does use the period for object/method
$db.execute("SELECT * FROM sessions", [], true))