使用registry.php存储对象
我的网站目前运行如下,
index.php
- 包含config.php->
config.php
设置变量,$siteRegistry =Registry::singleton();
还剖析 url 并根据 url 的内容创建新对象,因此 www.site.com/login 创建新的登录对象。
loginObject
在 config.php 内创建
创建新的登录模型
登录模型
- 需要访问 $siteRegistry 但显示以下错误,
注意:未定义的变量:F 中的 siteRegistry :\Projects\application\models\loginModel.php 第 37 行
这是第 37 行 -
$siteRegistry->storeObject("PDOExtender", "DBO");
我相信问题是它无法从 config.php 中找到 $siteRegistry 变量,有人知道吗知道如何解决这个问题吗?
My website at the moment runs like this,
index.php
- includes config.php->
config.php
sets variable, $siteRegistry = Registry::singleton();
also disects url and creates new object depending what url says, so www.site.com/login creates new login object.
loginObject
created inside config.php
creates new loginModel
loginModel
- Needs to access $siteRegistry but shows following error,
Notice: Undefined variable: siteRegistry in F:\Projects\application\models\loginModel.php on line 37
This is line 37 -
$siteRegistry->storeObject("PDOExtender", "DBO");
I believe the problem is that it can't find the $siteRegistry variable from config.php, does anyone know how to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于注册表(无论它是什么)是一个单例,因此无需将此 $siteRegistry 变量用作全局变量。
事实上,您应该始终在要使用的每个函数中调用
Registry::singleton
。如果太长,只需创建一个小包装函数:
单例对象的全部目的是一次始终只有一个对象,因此您可以在程序执行期间再次获取它们,而无需使用变量。
As Registry (what ever it might be) is a singleton there is no need to use this $siteRegistry variable as a global.
In fact you should always call
Registry::singleton
inside each function you want to use it.If that is to long just create a small wrapper function:
The whole purpose of singleton objects is that there is always only one of them at a time, so you can always get them again during the programs execution without having to use variables.