public static vs static public

Ever since starting with PHP 5 object oriented development, all documentation I read on the topic seemed to suggest that the only way to write the method keywords is "public static". I've been following along those lines, and for a while I really thought any other order would trigger errors. Only recently I found out the other way round is actually nicer.

Even in current literature on PHP 5 object oriented development, you'll mostly see mentions of the "public static" order. And it isn't strange, I can understand that some people prefer the PPP at the start to give clarity on the access rules for methods.

While looking around inside the symfony code, I first encountered the "static public" order of keywords. At first, I thought this strange, but the more I look at it, the more beautiful I think it is. It gives a great overview of which methods are static and which aren't. 

In the end it is of course a personal preference, however I've found my preference shifting towards the latter over the past months. I decided to check which was preferred most in my twitterverse, and here's the results:

 

As you can see, a big majority still prefers "public static". However, even more important I think is that the "static public" had more ground than I expected (or perhaps it's that a lot of people that follow me come from the symfony world). Having 2 votes for the option "I don't use static methods" was perhaps also a small surprise, but I guess it depends a lot on what you work on as well.

If you haven't voted yet, feel free to give your opinion in my poll. 


Add comment

Comments

gravatar Jan Schneider: A bit more representative, but somehow similar numbers: Google Code Search gives 45000 to 10200 for "public static" vs. "static public".
January 26, 2009
gravatar Evert: Funny! I've been wondering about the same thing.

My codebase:

public static = 2
static public = 12


January 26, 2009
gravatar Sara Golemon: My personal preference is 'public static', but I'm not strict about it, and I don't have it included in my project's coding standards.

Stats:

49.4% public static
34.1% static public
9.0% protected static
3.8% static protected
2.0% private static
1.6% static keyword only (implicit public)
0.1% final public static (a personal favorite)
January 27, 2009
gravatar Alvaro Videla: I voted for the public static just because is what I'm used to it. There is a new PHP developer on our time that came from the JAVA land and all his static methods are static public ... So know, depending on which classes I work, is the style I pick.
January 27, 2009
gravatar cx42net: I think the most important is not to write in a manner on in an other one, but to stick with one of it.

Like Alvaro Videla said, it's all about coding standards.
You cannot have your class written with a part starting by "public static" and the rest starting by "static public", it's so non professionnal ! :)
January 27, 2009
gravatar PiccoloPrincipe: I prefer not to use static methods, that seems like more procedural than OOP.
January 27, 2009
gravatar uli: @PiccoloPrincipe:
so, how can you be able to implement design patterns such as the singleton pattern without static methods and members? Not for nothing every oo language has this language construct.
January 27, 2009
gravatar Nick Belhomme: I prefer
public static function. I always put my static functions in the top of the class.

But Maybe I should switch as when I see static public, it gives more clarity. But then again habits die slow()

Nick
January 27, 2009
gravatar Perry: I think for most programmers they will be akin to the "public static" declaration. In C++ when you are defining classes, you place the "public:" before any variable/function declarations, eg:

public:
static int x;
char *ptr;

Keeping the public in front of the static gives you a more "cross language" discipline in my opinion.
January 27, 2009
gravatar Foo: I don't use public :) since a member is per default public.
Then you immediately see that a member is static or not.

January 27, 2009
gravatar Paul Davis: @uli
"so, how can you be able to implement design patterns such as the singleton pattern without static methods and members?"

The only static method in a Singleton should be the one that gives you the instance. None of the other methods should be static.
If so, you are not implementing a Singleton, you're just creating a class with static methods.

Even then, the singleton doesn't necessarily need to have a static instance method, the singleton could also be provided by a factory class, in which case, the singleton would have no static methods (or even be aware that it is a singleton).
January 27, 2009
gravatar left: Foo: even when a method or property is per default public, I think it is good practice to explicitly define it as public. That makes your code more clear. If you omit the 'public' part, then it might also be that you've forgotten to specify the access rules to the said property or method. However, when you explicitly state it to be public, you for sure know that it is public and is meant that way.

Paul Davis: It would be very hard to implement a singleton without at least the getInstance() method which should be static. Your factory can only extend a single class, making it a bit of a useless factory (as it would only be able to "produce" a single class instance) - unless of course the other classes are not singletons. Or am I overlooking another way to do singleton?
January 27, 2009
gravatar cx42net: Paul Davis: And in the case that you use a Factory pattern instead of a Singleton pattern, the Factory should also implement static method (at least one).

You cannot ignore using static methods. Java have static method, .Net framework have static method. In fact, all frameworks uses, somewhere, static methods.

Yes, for sure, you can use static method in class in kind of namespace for Php < 5.3, but if the "static" way is always present is OOP mean that it is important.
January 27, 2009
gravatar PiccoloPrincipe: @cx42net, @uli:
As a TDDer, I try to not use singleton as they carry global state trought the application. This aids decoupling and test in isolation of classes. If a class use a singleton, it lies about its dependencies since it access other objects that are not passed to it in the constructor; moreover,you can't mock out the implementation for unit testing.
(search Dependency injection on google for further explanation, or the google testing blog)
January 28, 2009
gravatar Johny: is there any difference between ?
I would say static first, cause static is the more important property,
cause property to mark it is callable outside context,
but i think i used both ways in past cause i am always thinking which is the more important one.

January 28, 2009
gravatar ruisilva: I've the same reaction as you when i look at symfony code.
static public looks better. :)
January 30, 2009

Php5_zce_logo

not tested in IE


Upcoming events

I will be speaking 08-10-2010: Symfony Day Cologne 2010
I will be speaking 09-10-2010: Symfony workshop

Tags

1337 2008 2010 4developers access modifiers accessibility AdaLovelaceDay09 advent agavi agile amsterdam apache apple article articles atk atkMetaNode audioscrobbler azure backwards compatibility barcelona bbc bbq beatstad belgium best practices bittorrent boards of canada book books bughuntday caching cake cal evans career cat cerf certificate cfp clear cms cologne common sense communities community conference conferences continuous integration contribute crisis css custom datetime DbFinderPlugin decorator decorators deployment devdays development directoryindex documentation download dpc dpc09 dpc10 DPC2008 dreamhost dv7 eclipse ed efficiency enterprise errors event events expertise ezcomponents facebook flickr framework frameworks freelance freeze frontend fun games germany getting real google googletalk graceful degradation hack hackers hidden gem hiphop howto hp html http ibuildings icann ide idm imovie indy ingewikkeld internet IPC ipc ipc08 javascript job jobeet john peel joomla kubuntu left on the web lighttpd lime linux live london loudblog m2ts mac malware mambo marjolein mediterra meeting meme meta methodology microsoft movie music mysql namespace namespaces netbeans netherlands nllgg odmarco open source opinion ORM osx paradiso pavilion pear performance personal pfc10 pfcongres pfcongrez photo php phpabstract phpazure phpBB phpbb phpbelgium phpbenelux phpbnl10 phpgg phpitalia phpnw phpnw08 phptek phptek09 phpuk2009 phpUnderControl phpunit php|architect php|tek podcast politics portability postcrossing presentation presentations private projects protected public qa recruiting refactoring review rewrite ruby on rails schedule scifi script security seven things sfdaycgn simplexml slides smfony software sogeti solar sound standard standards star trek static steer strings subversion symfony Symfony2 symfonycamp symfonyday symfonyUnderControlPlugin talk talks technology techportal tek09 telecommuting terratec terrorism testfest testing textpattern tips tld tomas training twig uncon unet usability usergroup validation vhost video vinyl virus warp weblogging wiki windows winphp women work workshop world world of warcraft wpi writing xml xpath xsd yara year youtube ZCE zemanta zend zend framework zend server zend studio Zend_Form
© 2004 - 2010 Stefan Koopmanschap + Powered by Symfony, photos powered by Flickr, links powered by Delicious, Shanghai smilies by Iconbuffet. Feeds: rss / atom. Left on the Web v4.4.0.1