Jump to content

[SOLVED] Include Class inside another Class


ykarmi

Recommended Posts

Hey Guys,

This is a pretty newbie question but my google keyword skills did not seem to help.

I am very new to OOP - I used to do everything procedural until not a long time ago.

 

 

I have a database class that I use to connect and talk to my database.

I have another class (user class) which is supposed to add a user to the database.

I need to somehow make my user class use the db class, and trying to include it with the __construct thing did not work...

This is what I tried

 

 
class User{
   
    public $error;
    public $db;
   
    public function __construct(){
        require_once "db.class.php";
        $this->db = new DB;
    }
   
    public function create($username, $password){
        //...
    }

}

 

 

If I use 'extends' for the DB class (though it's not really extending it) will I be prevented from extending the User class?

I use PHP5 and I have no idea how to do this in a way that is considered good practice.

 

Please help.

Thanks!

Yuval :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.