Answer the question
In order to leave comments, you need to log in
What is the correct way to use $bd in OOP?
Hello!
I am writing an API that will be used by a client desktop application. But the question is general, according to OOP.
I use the Fat-Free framework.
In index.php, it connects like this: $f3 = require('lib/base.php');
And it connects to the database like this:
$db = new DB\Mongo($f3->get('dbhost'),$f3->get('dbname'));
class MyClass {
public $f3;
function __construct($f3) {
$this->f3=$f3;
}
function MyFunc() {
$this->f3->somebody();
}
}
$obj= new MyClass ($f3);
Answer the question
In order to leave comments, you need to log in
Everything is simple. You're a bit confused about scopes)
class MyClass {
private $f3, $db;
function __construct () {
global $f3, $db;
$this->f3 = $f3;
$this->db = $db;
}
function MyFunc () {
$this->f3->somebody ();
}
}
$obj = new MyClass ();
Try the DI pattern, here is a very simple library: pimple.sensiolabs.org You
describe the dependencies of all classes, and she creates them herself.
Registry, Dependency Injection, Singleton (now they will start throwing poop) - choose any pattern.
There is such a thing as a singleton. An example can be viewed here .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question