views:

29

answers:

2

Hi all,

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.

Thanks!

A: 

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.

Knowledge Craving
Thanks for the quick response KC!I am not sure if I understand what you mean though. You said you cannot write anything else in the class definition, but then said that you are allowed to define methods?Also, I don't see why it'll print_$(this) would work, but getErrorString("blah") would not.Cheers!
lyf
@lyf - Since your function definition is not binded within a class definition, so the use of "$this" within the function definition will not work.
Knowledge Craving
+1  A: 

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();
former