Inspired by this post at PHPBuilder, I came up with this idea for making any public method or variable of a class accessible like a static attribute via the “::” operator. It’s read-only and not truly “static”, since the values of any class variables would always be the default value; but it does allow you to access variables and methods without having to instantiate an object (or actually having the object instantiated more-or-less in the “background”).
class myDateTime
{
public static function get($attr)
{
$dt = new DateTime();
if(method_exists($dt, $attr))
{
return $dt->$attr();
}
elseif(property_exists($dt, $attr))
{
return $dt->$attr;
}
else
{
throw new Exception("Invalid attribute '$attr'");
}
}
}
// sample usage:
echo myDateTime::get('getOffset');
As to whether this would ever have any real practical application beyond what was discussed in that forum thread, or whether this would be considered an abomination unto the gods of OOP I do not know. But I thought it was cool, so here it is.
