Kohana 全局变量被清除了吗?

发布于 2024-11-17 01:56:41 字数 530 浏览 0 评论 0原文

我之前写了一个问题,试图绕过在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾夏年华 2024-11-24 01:56:41

在这两种情况下,您都需要为变量指定 global

//controller.php
require_once("ccfunctions.php");

//ccfunctions.php
global $test;
$test = 'something';
function test(){
  global $test;
  echo $test;
}
test();
//This does not produce anything

顺便说一句,这确实是很奇怪的做法,我相信在不使用 global 的情况下,任何情况都有解决方法

You need to specify global for variable in both cases:

//controller.php
require_once("ccfunctions.php");

//ccfunctions.php
global $test;
$test = 'something';
function test(){
  global $test;
  echo $test;
}
test();
//This does not produce anything

Btw, it is really weird practice and I believe there are workarounds for any case without using global

一杯敬自由 2024-11-24 01:56:41

不要使用全局变量。当您想使用全局变量时为什么要使用 OOP 框架?

Don't use globals. Why use an OOP framework when you want to use globals?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文