Using an object in methods of other objects

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 other other methods of other classes. But what is the best way to access this object?

Create a new object each time?

Use global in the method?

global $db;

Or deliver it as an argument?

$object = new exampleClass($db);
0
source share
1 answer

Definitely:

$object = new exampleClass($db);

Or you can use the class registryto store objects, and then retrieve them if necessary. Someone will ring that this is bad practice, but good:

$object = Registry::get('db');
+3
source

Source: https://habr.com/ru/post/1529106/


All Articles