Is using an instance of a class to access static methods considered bad practice?
I.e.
$model = new MyClass();
$options = MyClass::getOptions();
vs
$model = new MyClass();
$options = $model::getOptions();
($model is instantiated in either case, I’m just wondering if one approach is preferable to the other.)
Traditionally the first way (specifying the class name itself) has more similarities with other languages such as Java.
The second one is unique to PHP (afaik); it works because the
::operator disambiguates the expression, unlike Java where a period is used for both instance and static properties.I’m not sure what the performance impact is by using the second option, but I think it comes down to personal taste / coding standards.
Conclusion
If the types of your instances are immediately clear from the surrounding code it might be easier to go for the second option (sometimes the class name can be pretty big); but if not, use the first option as it’s the most explicit.