Zend Framework 视图脚本、函数和变量范围

发布于 2024-09-12 23:39:55 字数 884 浏览 10 评论 0原文

我对 ZendFramework 相当陌生,正在尝试解决这个问题。在我的视图脚本 (index.phtml) 中,我有这样一段代码:

<?php

function getErrorString($element)
{
echo "<pre>";
print_r($this);
echo "</pre>";

 $string = '';
 if(!empty($this->error[$element]))
 {
  $string = $string.'<label class="error" for="'.$element.'" generated="true">';
  foreach($this->error[$element] as $error)
  {
   $string = $string.$error; 
  }
  $string = $string.'</label>';
 }
 return $string;
}

echo "<pre>";
print_r($this);
echo "</pre>";
getErrorString("blah");
die();

这给了我:

 Fatal error: Using $this when not in object context in index.phtml on line XX

在我看来,当您在视图中创建函数时,您会丢失 $this 变量。我确实在网上进行了搜索,但我看不到其他人试图实现我正在做的事情(可能性极小,也许我搜索错了)。

根据过去开发其他应用程序的经验,我看不出为什么应该将此函数放在单独的帮助程序中的充分理由 ->特别是因为这是该函数唯一被调用的地方。

任何想法将不胜感激。

I am rather new to ZendFramework and am trying to figure this out. In my view script (index.phtml), I have this bit of code that says:

<?php

function getErrorString($element)
{
echo "<pre>";
print_r($this);
echo "</pre>";

 $string = '';
 if(!empty($this->error[$element]))
 {
  $string = $string.'<label class="error" for="'.$element.'" generated="true">';
  foreach($this->error[$element] as $error)
  {
   $string = $string.$error; 
  }
  $string = $string.'</label>';
 }
 return $string;
}

echo "<pre>";
print_r($this);
echo "</pre>";
getErrorString("blah");
die();

That gives me:

 Fatal error: Using $this when not in object context in index.phtml on line XX

It seems to me that when you create a function within a view, you lose the $this variable. I did search around the net, and I can't see anyone else trying to achieve what I am doing (highly unlikely, maybe I'm searching it wrong).

With past experience developing other apps, I can't see a good reason why this function should be placed in a separate helper -> especially since this is the only place the function will ever be called.

Any ideas would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

迷爱 2024-09-19 23:39:55

您的函数 getErrorString() 不是 Zend_View-Object 的对象方法。
它有自己的作用域,无法到达 $this。

以下代码应该在index.phtml中适合您

function getErrorString($viewObject, $element)
{
echo "<pre>";
print_r($viewObject);
echo "</pre>";

 $string = '';
 if(!empty($viewObject->error[$element]))
 {
  $string = $string.'<label class="error" for="'.$element.'" generated="true">';
  foreach($viewObject->error[$element] as $error)
  {
   $string = $string.$error; 
  }
  $string = $string.'</label>';
 }
 return $string;
}

echo "<pre>";
print_r($this);
echo "</pre>";
getErrorString($this,"blah");
die();

Your function getErrorString() isn't an objectmethod of the Zend_View-Object.
It has it's own scope and couldn't reach $this.

The following code should work for you in the index.phtml

function getErrorString($viewObject, $element)
{
echo "<pre>";
print_r($viewObject);
echo "</pre>";

 $string = '';
 if(!empty($viewObject->error[$element]))
 {
  $string = $string.'<label class="error" for="'.$element.'" generated="true">';
  foreach($viewObject->error[$element] as $error)
  {
   $string = $string.$error; 
  }
  $string = $string.'</label>';
 }
 return $string;
}

echo "<pre>";
print_r($this);
echo "</pre>";
getErrorString($this,"blah");
die();
隔纱相望 2024-09-19 23:39:55

最后一次使用“$this”变量可能是显示致命错误的主要原因。这是非常合理的,因为除了定义方法和方法之外,您不能在类定义中编写任何其他内容。与该类相关的属性。

此外,如果您在视图页面中创建任何函数,则在该函数中,默认情况下无法访问“$this”变量。因此,您必须使“$this”变量成为全局变量,或者您需要在函数定义之外打印与“$this”变量相关的所需部分。

echo "<pre>";
print_r($this);
echo "</pre>";

因此,当您在函数定义中编写上述代码时,PHP 解析器无法找到此“$this”变量的任何对象上下文。这并不是说您丢失了“$this”变量,而是它将无法访问,而是丢失了逻辑。

希望有帮助。

The last use of "$this" variable is probably the main reason for showing the fatal error. It's quite justified because of the fact that you cannot write anything else in the class definition, except defining methods & properties with respect to that class.

Also if you are creating any function in a view page, then within that function the "$this" variable is not accessible by default. So you will have to make that "$this" variable go global or you need to print the required part, related to "$this" variable, outside the function definition.

echo "<pre>";
print_r($this);
echo "</pre>";

So when you are writing the above code in the function definition, the PHP Parser is unable to find any object context for this "$this" variable. It's not that you are losing that "$this" variable, but it will not be accessible, but to the missing logic.

Hope it helps.

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