It is clear to me that there are valid reasons to say yes to this proposal, but there are a lot to say no too. Even if it could be interesting in theory, due to of how PHP handles collisions between classes and functions names (no check at all), implementing a callable class could break existing code.
At the moment a class and a function with the same name can coexist, but implementing a callable class would mess this state of affairs. With the adoption of the namespaces, I think that this collisions could became so rare to not exist, but of course we cannot ignore pre existing situations. As suggested by Patrick Schaaf, a workaround is to implement a function, with the same name of the class, that behaves like a wrapper for a method call. It is a good point, but has the negative side that it must be implemented for every class who follow this pattern. It is not usable in a framework for example. It seams that to handle consistently a callable class is to modify PHP to treat functions/classes names case sensitive and to trigger an E_WARNING (or similar) in case of collision. In this way new code could take advantage of the new syntax and old code could simply suppress the warning. What do you think? 2013/3/19 Matīss Roberts Treinis <mrtrei...@gmail.com> > Bruno, > > Poor or questionable design or language use is not what this discussion is > about. PHP's greatest strength is its flexibility - PHP does allow to do > silly and sometimes, questionable things. Trying to limit this flexibility > is like trying to limit PHP, which is obviously bad thing to do. Example > given by Pierre clearly shows possibility of such code, and in fact, I have > seen such code myself in some applications where methods are shorthand's to > specific functionality provided as the class with same name. I even approve > such use and believe it can be quite handy for utility functions which does > some specific task and where you do not want your function to be miles > long, so you split it to handful of methods and wrap them in class. > > These proposed changes can potentially break a lot of peoples work without > a real profit to the language or its users. By my opinion, syntax > considering function and object calls should stay separated to ensure clean > and convenient way to properly identify one from another. Also, there are > no or never has been clear and unified standard by PHP for how to name > things in PHP. If one chooses to name his functions with names similar or > exactly like classes, he must be able to do so. > > 2013/3/19 Pierre du Plessis <pie...@pcservice.co.za> > > > > Le Tue, 19 Mar 2013 09:37:43 +0200, Pierre du Plessis a écrit : > > > > It would break existing software if you have a class and function > with > > > > the same name. > > > > Consider the following example: > > > > > > > > class foo { > > > > } > > > > > > > > function foo() > > > > { > > > > > > > > } > > > > > > > > Now what if you have several calls to foo() in your existing > > > > application? With the new change, will those calls call the function, > > or > > > > invoke the __invokeStatic method if it is available? > > > > > > It will clearly technically lead to a problem. Now I don't see why in > the > > > world a class and a function could share the same name. Regarding > > > conventions, a class name should begin with an uppercase character and > a > > > function a lowercase one. I don't see any relevant use case where a > class > > > and a function should share the same name. > > > > > > > > PHP doesn't care about uppercase or lowercase when it comes to function > > names. > > Have a look at http://3v4l.org/cePT5 for an example. > > And not everybody uses conventions anyway (although they should) > > > > Although it is not common to have a class and function have the same > name, > > it can happen. > > Take the following code as an example. > > > > class Debug { > > public static dump($var) > > { > > // some code to debug $var > > } > > } > > > > function debug($var) > > { > > return Debug::dump($var); > > } > > > > The problem is clear if you have a __invokeStatic method in the debug > > class, and you want to make a call to debug(). > > >