开发者

How to pull all instance variables of an object as local variables into the scope of an method from that object?

开发者 https://www.devze.com 2022-12-15 10:04 出处:网络
I have an class with some instance variables. It\'s purpose is for templating. So this class has a fetchTemplate() method which includes a PHP template file.

I have an class with some instance variables. It's purpose is for templating.

So this class has a fetchTemplate() method which includes a PHP template file.

In my template file, I can access any variable that has been assigned to the template, by writing:

echo $this->theVariable

If there was a way to "pull" all arbitrary instance variables into the local variables scope of fetchTemplate(), it would be possible to simply write:

开发者_Go百科
echo $theVariable

I slightly remember that there was something....


You can cast the object to an array and use extract():

extract( (array)$this );

but I think it will just work on public variables.

0

精彩评论

暂无评论...
验证码 换一张
取 消