Ok so i have the following code setup which seems to work fine:
user handler “module”:
class user_handler
{
private $dbo;
public function __construct($dbo)
{
$this->dbo = $dbo;
}
public function user_table_method()
{
$this->dbo->generic_db_method();
}
}
the connection class:
class connection
{
private $dbc;
public $user;
public function __construct()
{
$this->dbc = 'connection';
$this->user = new user_handler($this);
}
public function generic_db_method()
{
echo '<p>doing stuff with ' . $this->dbc . '</p>';
}
}
Then i can access user handler methods like so:
$dbc = new connection();
$dbc->user->user_table_method();
My question is this:
Would it be possible with a method in the connection class to create the _user object only when it’s required?
What i’m thinking of is a method that would be used like so:
$dbc->add_handle('user', 'user_handler');
Which would create a new public property called user from scratch and set it as a new
user_handler object:
// in more detail
public function add_handle($name, $module)
{
if(!isset($this->/*somehow use $name*/))
{
$this->/*somehow use $name*/ = new /*somehow use $module*/();
}
}
..so the new way to set it up would be like:
$dbc = new connection();
$dbc->add_handle('user', 'user_handler');
$dbc->user->user_table_method();
Thanks in advance! (looking at php 5.2 and up)
Then you would just do similar to how you were thinking:
I’ve also renamed it from $_user to $user as I mentioned in the comment since it is public.