I have the following, however I’m unable to access the database functions outside of the initial db class?
Thanks!
database.php
class db
{
private $connection;
public function __construct()
{
$this->connection = new PDO();
}
}
admin.php
class admin
{
private $connection
public function __construct(db $connection)
{
$this->connection = $connection;
}
function myFunc()
{
// How do I access the connection here?
}
}
main.php
//include db.php
//include admin.php
$connection = new db();
$admin = new admin($connection);
// How do I access the DB here?
First of all, why are you encapsulating
PDOjust to class containing that one object? Cannot you usePDOdirectly?One of the common practices would be to implement getter in db class, like:
Another way is to re-implement every function (why would you do that?!), or use
__callmagic function…Or just make
$connectionpublic 😉Or you could extend
PDOclass (I’m not sure whether it’ll work):