Why can’t I do this:
class MyClass{
public $dir = 'root/'.Util::getDir();
public function getURL($file){
$fullUrl = $this->dir . $file;
return $fullUrl;
}
}
echo MyClass::getUrl('my.pdf');
Basically, the issue is with the second line. I can’t call a static method while declaring a variable in a class.
You can’t do this because the language doesn’t support it.
In the PHP5 object model a method is either static or dynamic. You have to choose. Ditto class properties.
However, there is nothing to stop you using a private class variable and using object overloading by declaring a __get() to call static methods either in class or out of class.. Just do an isset test the private variable and call your static method on the first invocation.
Remember that you can always refer to static properties and methods using the
self::construct.