Kohana 全局变量被清除了吗?
我之前写了一个问题,试图绕过在 Kohana 中使用控制器,但我放弃并重写了该文件。现在,我在 Kohana 控制器中有这个 php 脚本。控制器正在呈现的视图“需要”一个 php 文件,以便我可以执行一些第三方功能。我遇到了一个重大问题并开始调试。问题似乎是包含文件内的函数无法从同一文件内访问函数外部的变量。看来 Kohana 正在以某种方式清除全局???
例如:
//controller.php
require_once("ccfunctions.php");
//ccfunctions.php
$test = 'something';
function test(){
global $test;
echo $test;
}
test();
//This does not produce anything
对此有什么想法吗?
编辑: 实际上,即使在我正在渲染的视图文件中,上面的示例也不起作用。忘记包含的文件。我意识到 Kohana 试图强制执行 MVC 模型,但这很荒谬。为什么我不能创建函数并从视图文件中调用全局变量?
I wrote a question earlier trying to get around using a controller in Kohana, but I gave in and rewrote the file. So now, the I have this php script inside a Kohana controller. The view that the controller is rendering is 'requiring' a php file so I can perform some third-party functions. I was having a major issues with it and got to debugging. The problem seems that functions inside the included file cannot access variables outside the function from within that same file. It seems that Kohana is clearing the globals somehow???
example:
//controller.php
require_once("ccfunctions.php");
//ccfunctions.php
$test = 'something';
function test(){
global $test;
echo $test;
}
test();
//This does not produce anything
Any thoughts on this one?
EDIT:
Actually, the above example doesn't work even from my view file that is being rendered. Forget the included file. I realize Kohana tries to enforce the MVC model, but this is ridiculous. Why cannot I not create a function and call a global variable from within my view file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这两种情况下,您都需要为变量指定
global
:顺便说一句,这确实是很奇怪的做法,我相信在不使用
global
的情况下,任何情况都有解决方法You need to specify
global
for variable in both cases:Btw, it is really weird practice and I believe there are workarounds for any case without using
global
不要使用全局变量。当您想使用全局变量时为什么要使用 OOP 框架?
Don't use globals. Why use an OOP framework when you want to use globals?