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.
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);
}
}
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 '...';
}
}