Digging through Symfony 1.4

Hi,

I’m wondering if anyone knows how Symfony does the following:


class someActionClass extends sfActions {
  public function executeLists() {
    $this->fname = 'John';
    $this->lname = 'Doe';
  }
}

And then when the view is called, all I need to do is echo $fname / $lname to get the value of those variables. What confuses me is that they are using $this as of it was a property and then some how it changes to just a variable.

If anyone can clear that up please :injured:

I don’t know how SF does it, but one way to it would be to not use instantiated class properties (i.e. no public $lname; in the class) but set and get the values of virtual properties in an array and then export extract that array when you render the view file.

Sounds more confusing than it is:


class MyClass
{
   private $properties;
   
   public function __set($key, $value)
   {
       $this->properties[$key]=$value;
   }

   public function __get($key)
   {
       return $this->properties[$key];
   }

   public function render($viewFile)
   {
      extract($this->properties);
      include($viewFile);
   }
}

Something like that :slight_smile:

Ah yes, that would work! BTW, did you mean extract() ?

:blush: I did


class TestThing {
  public $fname = 'John',
         $lname = 'Doe';
}

$c = new TestThing;
extract( (array)$c ); # Typecasting to array probably not required.

var_dump( $fname, $lname );

# This might also work:

class TestThing {
  public $fname = 'John',
         $lname = 'Doe';

  function load ()
  {
    extract( (array)$this );
    include '...';
  }
}

Yes, that would also work. But I don’t really like it because it also extracts private variables, and I like my private variables private :slight_smile:

Only if you use “$this” if done outside of the class it will only have public parameters.