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
gravatar write proposals: Thank You for unique information!

May 5, 2011
gravatar online dating sites: you, what expertise do you have on this issue? Make us laugh a little! Already when I read your initial reactions, I feel we are in a good way to big lol!
October 14, 2011
gravatar paxil lawsuit: The development is so good behind this is so much. There is so much that you can get from this. Keep up the good work.
October 28, 2011
gravatar Cruise deals: I see so much that you can do with it. I see a great future from using this. I see so many great things coming from this.
October 29, 2011
gravatar buy research papers: That's exactly what I was looking for! Your article is much useful for my paper, thanks a lot
December 5, 2011
gravatar essay writing guide: Boredom flourishes too, when you feel safe. It's a symptom of security.
December 8, 2011
gravatar weight loss: The war between being and nothingness is the underlying illness of the twentieth century. Boredom slays more of existence than war.
December 8, 2011
gravatar Ugg Boots Sale: It gives a great overview of which methods are static and which aren't.
December 8, 2011
gravatar gwswfm: I prefer not to use static methods, that seems like more procedural than OOP.

December 23, 2011
gravatar free screensavers fishes: Completely FREE screen savers for Windows. We have handpicked and tested some of the best free screensavers around: no expiry, no nag messages, Screen savers and wallpapers for Windows and Mac including OS X. Collections consist of images from artists and photographers.
January 21, 2012
gravatar discount uggs boots: I am typically to running a blog and i really respect your content. The article has really peaks my interest. :P
February 1, 2012
gravatar michael kors purses: I am absolutely amazed at how terrific the stuff is on this site. I have saved this webpage and I truly intend on visiting the site in the upcoming days. Keep up the excellent work!

February 2, 2012
gravatar IT Support Firgrove: These kind of post are always inspiring and I prefer to read quality content so I happy to find many good point here in the post
February 3, 2012

Php5_zce_logo

Upcoming events

I will be speaking 06-02-2012: D-Day
I will be speaking 17-02-2012: Techademy Trainingday February
I will be speaking 23-02-2012: Zend Webinar: Git for Subversion Users

Tags

1337 2008 2010 2011 4developers access modifiers accessibility AdaLovelaceDay09 advent agavi agile alfred amsterdam apache api apple article articles atk atkMetaNode audioscrobbler automation azure backwards compatibility barcelona barcodes bash bbc bbq beatstad belgium best practices bittorrent blogging blogs boards of canada book books bughuntday bundle caching cake cal evans calendar career cat cerf certificate cfp clear cms cologne common sense communities community components conference conferences contest continuous integration contribute contribution crisis css custom d-day datetime DbFinderPlugin decorator decorators deployment devdays development directoryindex docblox doctrine documentation download dpc dpc09 dpc10 dpc11 DPC2008 dreamhost drupal dv7 eclipse ed editors efficiency enterprise errors event events expertise ezcomponents facebook finland flickr fork framework frameworks freelance freeze frontend fun game games geoip germany getting real git github gnome-do google google calendar googletalk graceful degradation hack hackers hidden gem hiphop howto hp HR html http i386 ibuildings icann ide ideasofmarch idm imovie indy ingewikkeld integration international php conference internet interview ipad IPC ipc ipc08 ipc10 ipc11se iterm2 javascript jenkins jenkins-php job job openings jobeet john peel joomla joomladays kiva kubuntu launcher launchy left on the web libraries library lighttpd lime linktuesday linux live london loudblog m2ts mac magazines malware mambo marjolein mediterra meeting meme meta methodology micro-financing microframework microsoft migration movie music mysql namespace namespaces netbeans netherlands newsfire nllgg nos odmarco open source opinion ORM osx paradiso paris partnership pavilion pear pecl performance personal pfc10 pfc11 pfcongres pfcongrez pfz photo php php5.3 phpabstract phpazure phpBB phpbb phpbelgium phpbenelux phpbnl10 phpday phpdoc phpdocumentor phpgg phpitalia phpnw phpnw08 phpnw11 phpstorm phptek phptek09 phpuk2009 phpUnderControl phpunit php|architect php|tek podcast politics portability postcrossing presentation presentations private projects protected prototype PSR-0 public python qa qr codes re2c recruiting refactoring review rewrite ruby on rails san francisco schedule scifi script security sensio seven things sfdaycgn sflive2011 shell scripting silex simplexml slides smfony software sogeti solar sound speakers spl ssh standard standards star trek static steer strings stylesheets subversion symfony symfony live Symfony2 symfonycamp symfonyday symfonylive symfonyUnderControlPlugin talk talks techademy technology techportal tek09 telecommuting terratec terrorism testfest testing textmate textpattern the right tool timeout tips tld todo tomas tools training twig uncon unet usability usergroup validation vhost video vim vinyl virus warp webinar weblogging webservices wiki windows winphp women wordpress work workshop world world of warcraft wpi writing wunderlist xml xpath xsd yara year youtube zc11 ZCE zemanta zend zend framework zend server zend studio zendcon Zend_Form zite
© 2004 - 2012 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