Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can PHP static methods legally have a visibility of protected or private?

Tags:

oop

php

class

I realize that it's possible to define a static class method as private and protected in PHP. This allows for an instantiated class, or public static method to access it's own private/protected static methods.

protected static function jumpOver  ()

However I'm not sure if this is legal in the sense of OOP design. I can't find any real info stating it's ok to do this. I'm worried PHP may "patch" this in future versions if this is not a valid and break my scripts.

like image 839
Mikey Avatar asked Jul 26 '10 16:07

Mikey


People also ask

Can static method be private PHP?

A static private method provides a way to hide static code from outside the class. This can be useful if several different methods (static or not) need to use it, i.e. code-reuse.

Can static methods be public or private?

Static methods can be public or private. The static keyword is placed right after the public/private modifier and right before the type of variables and methods in their declarations.

Can static methods be protected?

Usually there isn't much reason to declare a private or protected method as static. Static methods are usually public. But when the behavior of a method does not affect or is affected by the state of the current instance, declaring it static can be a way to document this fact.

Can you have a private static method?

Yes, we can have private methods or private static methods in an interface in Java 9. We can use these methods to remove the code redundancy. Private methods can be useful or accessible only within that interface only. We can't access or inherit private methods from one interface to another interface or class.


1 Answers

It is. Static methods are usually nothing more than helper methods that have code you possibly don't want to be public.

The other common object-oriented languages I can think of have it too (C++, Java, C#). I really don't think they're ever going to remove that feature.

Besides, the guys at PHP are slow at breaking existing features, so I wouldn't worry too much about it.

like image 55
zneak Avatar answered Sep 19 '22 17:09

zneak