Using a object in methods of other objects

一曲冷凌霜 提交于 2019-12-11 18:13:23

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!