2

Is it possible to use an object within an object? For example, I have a MySQL class which has every MySQL related method (connect, query, get rows...), and I'm making a new object called User, which will have methods such as login, logout, register etc... Is it possible to include my MySQL class, define an object and then run queries in my new object? If so, HOW? For example:

class User{
    $m = new MySQL();
    function login($username, $password, $remember = false){
                $m->rQuery("SELECT * FROM users......");
    }
}
1
  • 1
    Pro Hint: Pass your database object to User rather than having User instantiate it itself. It reduces coupling. Check out Dependency Injection Commented Nov 4, 2011 at 16:50

4 Answers 4

3

Is it possible to use an object within an object?

Yes.

Use the __construct function to initialize any sub-objects that you need

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

Comments

2

Yes, you can use it like this:

class User{
    private $db;

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

    function login($username, $password, $remember = false){
                $this->db->rQuery("SELECT * FROM users......");
    }
}

Comments

2

An answer that is very similar to the others, but with one - in my opinion, key - difference: rather than instantiating the dependency/dependencies in the constructor, pass them to the constructor:

class User
{
    protected $db;

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

    public function login($username, $password)
    {
        $this->db->query("SELECT * FROM users...");
    }
}

The benefits to this type of dependency injection include:

  1. It clearly demonstrates that the client/consumer class (User) has a dependency (Db).
  2. Reduces coupling between the User class and the Db class. This helps significantly when unit-testing the User class. You can create a mock Db that performs as expected. Any test failures are then clearly attributable to the User class.

Of course, in order to instantiate a User object, you need to instantiate a Db object first. With a lot of dependencies for a single object or for a long chain of dependencies, this can be kind of a pain. In that case, it is helpful - but not required - to employ a Factory or a Dependency Injection Container to assist with this object creation.

Comments

0

Ofcource and you can, but not in that way. What you can do is like that:

class User
{
    private $m = null;

    function __construct()
    {
        $this->m = new MySQL();
    }

    function login($username, $password, $rememer = false)
    {
        $this->m->rQuery("SELECT * FROM users......");
    }
}

6 Comments

Old style constructor. I'd favor __construct.
The time of you downgrade I changed the constructor name. Please uprate again if you like
May can I know why am I downrated ?
There's still another bug in your code. As soon as you fix it, your answer will be almost the same as Vikk's...
I have fix my bug, and also, you know very whel that every person will give almost the same answer. Even you, if you answer that question, will seem your answer to my one and Vikk answer too. If you see the submit time, these answer posted at the same time, so, neither copy Vikk answer or Vikk copy my answer.
|

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.