The best way to share database connection between classes

时光毁灭记忆、已成空白 提交于 2019-11-29 07:46:53

you can have a static method in your database class wich will return an instance of itself.

$db = DB::getInstance();

moreover you can implement a singleton pattern. you can read about it here.

PHP Patterns

The main idea is that you save your DB object in static property and then in getInstance check if it's set you return it or create new one, constructor should be made private so that the Object can't be created anywhere else but in getInstance.. this ensures that there is always one Instance of DB object.

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