Laire
Laire

Reputation: 1330

Using a 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 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);

Upvotes: 0

Views: 97

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

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');

Upvotes: 3

Related Questions