1

Here's my current code

class Database {
    //Connect to database here...
}

class User extends Database {

    public function GetUInfo($id) {
        $db = new Database;
        $db->query("SELECT `id` FROM `users` WHERE `id`=:id");
        $db->bind(":id", $id);
        return $db->single();
    }

    public function uptime($id) {
        $db = new Database;
        $db->query("UPDATE `users` SET `time`=".$_SERVER['REQUEST_TIME']." WHERE `id`=:id");
        $db->bind(":id", $id);
        return $db->execute();  
    }

}

In the User class, how can I make it so that $db = new Database; is ready in all of the public functions, instead of having to add $db = new Database; in each of the functions?

2
  • can we show Database code class ? as you have extends User class from Database Commented Jan 17, 2015 at 8:45
  • @AnandPatel - SO won't let me show it because it's rather large and it'd make the post majority code. Commented Jan 17, 2015 at 8:45

2 Answers 2

2

As you have extends Database Class into User why you are creating objects ? you can directly access functions(public and protected) of Database Class

you can directly access it using $this keyword.

only you have to make connection in Constructor(parent class) and in child class you have to call parent class constructor.

<?php
class Database
{
    protected $db;

    public function __construct()
    {
        //your connection code
    }
}

class User extends Database
{

    public function __construct()
    {
        parent::__construct();
    }

    public function GetUInfo($id)
    {
        $this->query("SELECT `id` FROM `users` WHERE `id`=:id");
        $this->bind(":id", $id);
        return $this->single();
    }

    public function uptime($id)
    {
        $this->query("UPDATE `users` SET `time`=".$_SERVER['REQUEST_TIME']." WHERE `id`=:id");
        $this->bind(":id", $id);
        return $this->execute();
    }
}

?>
Sign up to request clarification or add additional context in comments.

2 Comments

If you want to unit test this kind of code you're stuck with your database. IMHO is it better to push the database dependency inside the user class. Not saying your approach it's a bad thing per se, just keep it in mind :-)
as he have extends Database class to User that`s why ;) otherwise i dont post this answer
2

Create a constructor and private property $db

private $db;

public function __construct() {
  $this->db = new Database();
}

So you can use it like this

   public function uptime($id) {
        $this->db->query("UPDATE `users` SET `time`=".$_SERVER['REQUEST_TIME']." WHERE `id`=:id");
        $this->db->bind(":id", $id);
        return $this->db->execute();  
    }

As metioned mTorres its better to do like this

public function __construct(Database $db) { $this->db = $db; }

If you are new to OOP - read this article it might help.

5 Comments

This works, but I wonder if there's an easier option to avoid having to use $this->db. It's fine, but it'd be nice just to be able to use $db->
Yes, but you could make the dependency explicit and inject it instead of creating a new object directly: public __construct(DatabaseType $db) { $this->db = $db; }
@mTorres totally agree with you
@BryanParmenter, there's no other way, even if you extend your db (which is probably not a good idea), you MUST use $this->db, think that in order to type $db it must be a local (for the method!) variable, you could easily do $db = $this->db at the beginning of each method (but if you have a good IDE that shouldn't bother you)
Thank you. I'm still new to OOP and this has been very helpful.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.