在其他对象的方法中使用对象

编程入门 行业动态 更新时间:2024-10-28 15:31:40
本文介绍了在其他对象的方法中使用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在这种情况下,我在我的脚本的主文件中创建一个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');

更多推荐

在其他对象的方法中使用对象

本文发布于:2023-07-16 16:05:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1123392.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!