0

I'm trying to learn OOP, and some of its concept. I've following class for users:

    class Users
    {

        private $host   = DB_HOST;
        private $user   = DB_USERNAME;
        private $pass   = DB_PASSWORD;
        private $dbname = DB_NAME;

        private $conn;
        private $stmt;
        public  $error;

        function __construct()
        {
            $dsn = 'mysql:host='.$this->host.';dbname='.$this->dbname.';charset=utf8';
            $options = array(
                PDO::ATTR_PERSISTENT => true,
                PDO::ATTR_ERRMODE    => PDO::ERRMODE_EXCEPTION
            );
            try {
                $this->conn = new PDO($dsn,$this->user,$this->pass,$options);
            } catch (PDOException $e) {
                $this->error = $e->getMessage();
            }
        }

        private function mysql_execute_query($sql,$params)
        {
            $this->stmt = $this->conn->prepare($sql);
            $this->stmt->execute($params);
            return $this->$stmt;
        }

        public function find_user_by_provider_uid($provider,$provider_uid)
        {
            $sql = 'SELECT * FROM users WHERE provider = :provider AND provider_uid = :provider_uid LIMIT 1';
            $params = array(
                ':provider'     => $provider,
                ':provider_uid' => $provider_uid
            );
            $result = $this->mysql_execute_query($sql,$params);
            return $result->fetch();
        }
}

First of all is there some tip that comes to mind for structuring this code better? or using more features of oop?

Second, it fails with following error:

PHP Notice: Undefined variable: stmt PHP Fatal error: Cannot access empty property

Both of this lines refer to return $this->$stmt; inside mysql_execute_query

My hunch is that it has something to do with it being private function. But I cannot tell.

Any ideas?

7
  • You used $this->stmt in one case and $this->$stmt in another. Don't you see the difference? Commented Feb 9, 2014 at 9:48
  • @zerkms I'm trully blind. Commented Feb 9, 2014 at 9:49
  • @salivan please inject a complete PDO instance in your class, instead of creating new DB connection every time you construct an instance of Users. Also, why are you using emulated prepares? Commented Feb 9, 2014 at 11:15
  • 1
    @salivan yes, that's correct. The emulation was turned on by default because pre-5.1 MySQL version did not support prepared statements. You could say it was a very short-sighed patch for a temporary problem. PHP core team has this nasty fetish for backwards compatibility =/ Also on a different note, when you pass parameters through execute() they all are bound as PDO::PARAM_STR. You might want to look int PDO's bintParam() methods, since some of your values looks kinda like integers and should be bound as such. Commented Feb 9, 2014 at 14:00
  • 1
    No, I mean in the constructor. Basically you do $user = new Users(new PDO('mysql:...', $user, $pass)); or better: $pdo = new PDO(...); $users = new Users($pdo); $docs = new Documents($pdo); .. etc. This way you can share the same connection instance between multiple object. When you pass an object as parameter, it does not get copied. Commented Feb 9, 2014 at 14:31

1 Answer 1

3

Here the error:

return $this->$stmt;

But should be:

return $this->stmt;
Sign up to request clarification or add additional context in comments.

1 Comment

I repeat myself, I'm blind :|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.