PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
class myclass
{
public function methodname()
{
static $returnValue = '';
if (!empty(self::$returnValue)) {
return self::$returnValue;
}
//heavy computation here assigning self::$returnValue
return self::$returnValue;
}
}
$n = new myclass();
echo $n->methodname();
I get undeclared static property....of course I could move this static $returnValue = ''; to the class declaration and it will work...but really need this only for this one method. is it possible?
Thanks
class myclass
{
public static $returnValue = null;
public function methodname()
{
if (self::$returnValue !== null) {
return self::$returnValue;
}
//heavy computation here assigning self::$returnValue
return self::$returnValue;
}
}
yep, figured this out.
was thinking what might be the drawbacks of this usage though.
maybe more memmory usage or something...no idea yet...but prefer it compare to additional property in the class.