问题
in this case I create a sqlite3 object in the main file of my script:
$db = new sqlite3('file.sqlite');
now I need to access the sqlite file in different other methods of other classes. But what is the best way to access the object there?
Create every time a new object?
Use in the methode global?
global $db;
Or deliver it as argument?
$object = new exampleClass($db);
回答1:
Definitely:
$object = new exampleClass($db);
Or it is possible to use a registry class to store objects and then retrieve them when needed. Someone will chime in that this is a bad practice, but oh well:
$object = Registry::get('db');
来源:https://stackoverflow.com/questions/22056576/using-a-object-in-methods-of-other-objects