oop - php: skip '$this' in object class -
is way skip $this in onject? having class:
class klas { private $var1; private $var2; public function makeit() { echo $this->var1; } }
is way in way:
class klas { private $var1; private $var2; public function makeit() { echo $var1; } }
maybe somewhere kind of magic method or domething this?
if not need modify values, use them, can use extract
in combination get_object_vars
create local copies.
this define properties within current symbol table, letting refer them local variables. note changes them not affect corresponding object properties, these not references, new copies of variables.
<?php class klas { public $foo = 'world'; private $bar = 'my code'; public function makeit() { extract(get_object_vars($this)); print 'hello '.$foo.', velkommen '.$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 cast object array instead of using get_object_vars
:
$vars = (array)$this; extract($vars);
the documentation of extract
mentions extr_refs
flag, should extract references local symbol table. when cast object array, resulting array said references (in comments in php docs), but, not able reproduce on codepad, though version comparable referenced in claim. can give shot on installation, mileage may vary.
documentation
extract
- http://us1.php.net/manual/en/function.extract.phpget_object_vars
- http://www.php.net/manual/en/function.get-object-vars.php
Comments
Post a Comment