nathan
nathan

Reputation: 460

OOP - Function not defined?

I am trying my hand at OOP, and it has not been kind to me recently. Here is my class:

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;
}

}

When I try to define it and call the function "execute" I get this error:

Fatal error: Uncaught Error: Call to undefined function execute() in /var/www/html/api/classes/sessions.php:49 Stack trace: #0 /var/www/html/index.php(3): require_once() #1 {main} thrown in /var/www/html/api/classes/sessions.php on line 49

Here is how I am trying to call it (debugging)

$db = new db();
die(var_dump($db.execute("SELECT * FROM sessions", [], true)));

I don't see why this error is occuring. Could it be something with my php configuration? (it does not execute, even if I make dbexecute its own function and not an object)

Upvotes: 2

Views: 180

Answers (2)

Rasclatt
Rasclatt

Reputation: 12505

You have a concatenation . where you should have ->. Should be:

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

Instead of:

// 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))

Upvotes: 2

Jacob Mulquin
Jacob Mulquin

Reputation: 3608

What's happening is that var_dump() is attemping to concatenate the string $db with the output of the execute() function in global scope.

Upvotes: 0

Related Questions