If you do not need to modify the values, only use them, you can use extract in combination with get_object_vars to create local copies.
This will define any properties within the current symbol table, letting you refer to them as local variables. Note that any changes to them will not affect the corresponding object properties, as these are NOT references, but new copies of the variables.
<?php
Class klas
{
public $foo = 'world';
private $bar = 'my code';
public function makeIt()
{
extract(get_object_vars($this));
print 'Hello '.$foo.', velkommen to '.$bar;
$foo = 'Dave';
}
}
$inst = new klas();
$inst->makeIt();
print 'Hello '.$inst->foo; // not "Hello Dave"
Try it: http://codepad.viper-7.com/eKr1v7
You can also cast the object as an array instead of using get_object_vars:
$vars = (array)$this;
extract($vars);
The documentation of extract mentions a EXTR_REFS flag, which should extract references into the local symbol table. When you cast the object as an array, the resulting array is said to be references (in comments in PHP docs), but, I was not able to reproduce this on codepad, even though the version is comparable to that referenced in the claim. You can give it a shot on your installation, your mileage may vary.
Documentation
$classInstance->propertyis how you access regular non-static values in a class.$thisjust refers to the current instance of the class.