在其他对象的方法中使用对象 [英] Using a object in methods of other objects
本文介绍了在其他对象的方法中使用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这种情况下,我在我的脚本的主文件中创建一个sqlite3对象:
in this case I create a sqlite3 object in the main file of my script:
$db = new sqlite3('file.sqlite');
现在我需要访问其他类的不同的其他方法中的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?
在methode全局中使用?
Use in the methode global?
global $db;
或作为参数提交?
$object = new exampleClass($db);
推荐答案
当然:
$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');
这篇关于在其他对象的方法中使用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文