Arbit - project tracking

Dwoo

#49: Incorrect handling of object attributes accessing throught magic method __get

Issue revisions

  • closed by ggp at 2009-N-25 12:31
Type bug bug
State closed closed
Priority normal normal
Resolution fixed fixed
Assigned to Jordi Boggiano
Scheduled for 1.1.1
Affected versions
Affected components
Last change Wednesday 25 November 2009 12:31:38 UTC by ggp
If I try to access an object attribute through the magic method __get($attribute), dwoo gets the value but I can't use it in if blocks or other functions:

For example, I have this class:
<pre>
class testClass
{
  protected $_id;

  public function __set($attribute, $value)
  {
    $method = 'set' . ucfirst($attribute);
    if (method_exists($this, $method)) {      
      $this->$method($value);      
    }
  }

  public function __get($attribute)
  {
    $method = 'get' . ucfirst($attribute);
    if (method_exists($this, $method)) {
      return $this->$method();
    }
  }
  
  public function getId()
  {
    return $this->_id;
  }

  public function setId($id)
  {
    $this->_id = $id;
    return $this;
  }
}
</pre>

I create an instance and assign it an id:
<pre>
$test = new testClass();
$test->id = 4; // For example, 4
$data = array('test' => $test);
</pre>
Then I render the template:
<pre>
$dwoo = new Dwoo();
$dwoo->output('testTemplate.phtml', $data);
</pre>
The template 'testTemplate.phtml' is:
<pre>
{$test->id}
{intval($test->id)}
{if $test->id == 4} correct! {else} bug! {/if}

{$test->getId()}
{intval($test->getId())}
{if $test->getId() == 4} correct! {else} bug {/if}
</pre>


Expected result is:
<pre>
4
4
correct!

4
4
correct!
</pre>
However, the result I get is:
<pre>
4
0
bug!

4
4
correct!
</pre>
  • ggp at Wednesday 25 November 2009 12:34:24 UTC

    The testClass definition has been closed by this page editor. Of course, methods are inside the class.

  • ggp at Wednesday 25 November 2009 12:37:14 UTC

  • Jordi Boggiano at Wednesday 25 November 2009 12:45:04 UTC

    Not sure why this happens, but I'll try to look into it soon, I'm pretty sure it should work because I used it with Doctrine and other libs that do a lot of magic like that.

  • Jordi Boggiano at Wednesday 25 November 2009 12:46:15 UTC

    fixing description

  • Jordi Boggiano at Wednesday 25 November 2009 18:02:17 UTC

    I tried with your exact code and it works fine here.. Could you please check with an updated version from svn ? svn://dwoo.org/dwoo/branches/1.1 or svn://dwoo.org/dwoo/trunk

    Thanks

  • ggp at Thursday 26 November 2009 09:35:45 UTC

    I have tried my code in two different servers, and I've seen that it fails on the last stable release (1.1), but it works in the last trunk downloaded through svn (svn://dwoo.org/dwoo/trunk).

    Thank you very much and congratulations for your great work.

    Jordi Boggiano wrote: > I tried with your exact code and it works fine here.. Could you please check with an updated version from svn ? svn://dwoo.org/dwoo/branches/1.1 or svn://dwoo.org/dwoo/trunk

    > Thanks