# Dropping requirement for `function` keyword for methods in classes/interfaces/etc

- Thread: https://externals.io/message/65975
- Original archive: https://news-web.php.net/php.internals/65975
- Started by Nikita Nefedov on 2013-02-19T21:44:03+00:00
- Messages: 34
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/65975.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #65979 — johannes@schlueters.de — 2013-02-19T17:53:40+00:00

URL: https://externals.io/message/65975#65979 · In reply to: #65975

Hi,


> Hi!
> 
> As someone mentioned in the thread about short syntax for closures, we  
> could also drop requirement for `function` keyword when defining/declaring  
> methods in classes, interfaces or traits.

I proposed this in November '10: http://news.php.net/php.internals/50628

I agreed to the conclusion that the "function" keyword provided a nice
way to grep for functions when handling foreign code and leaving it out
only provides little improvement in less typing.

Please provide new arguments for a new discussion. (That thread was
rather long)

And as a general note I'd like to remind you all about Rasmus' recent
mail: http://news.php.net/php.internals/65894 - In my words: Why not fix
bugs instead of creating new ones?

johannes

---

## #65984 — Rasmus Lerdorf — 2013-02-19T18:41:41+00:00

URL: https://externals.io/message/65975#65984 · In reply to: #65983

> Hmm, I agree about grepping, but how often do you do it? Actually, last
> time I grepped php files was half a year ago I think, when I had just
> ssh connection and didn't want to mount sshfs. But usually there's IDEs
> that can statically analyze your code and let you search against huge
> codebases in seconds, I don't want to sound mean, but hey, does anybody
> greps php code?

About 20-30 times every single day.

-Rasmus

---

## #65986 — Lester Caine — 2013-02-19T18:53:37+00:00

URL: https://externals.io/message/65975#65986 · In reply to: #65984

Rasmus Lerdorf wrote:
> 
> > […]

> About 20-30 times every single day.

Ditto via the Eclipse equivalent ...

Has anything changed since we last discussed this? And threw it out ...

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #65987 — Levi Morrison — 2013-02-19T19:01:46+00:00

URL: https://externals.io/message/65975#65987 · In reply to: #65986

> Rasmus Lerdorf wrote:
> > […]
>
>
> > […]
>
>
> Ditto via the Eclipse equivalent ...
>
> Has anything changed since we last discussed this? And threw it out ...

Yes: the dynamics of the people who vote.

---

## #65989 — Rasmus Lerdorf — 2013-02-19T19:10:22+00:00

URL: https://externals.io/message/65975#65989 · In reply to: #65988

> Are you grepping for all the functions or you are grepping just for some
> specific function? If so, you are likely already know what visibility
> this function has, so couldn't you grep for `public %functionName%`
> instead of `function %functionName%`?
> At the end, you can always use `grep
> '(function|public|private|protected) functionName' file`, and if it's
> long to type, you can make sh script, or even alias.

public is the default visibility so it is often left off, so no, I can't
grep for that.

-Rasmus

---

## #65993 — johannes@schlueters.de — 2013-02-19T20:47:35+00:00

URL: https://externals.io/message/65975#65993 · In reply to: #65988

> 
> At the end, you can always use `grep '(function|public|private|
> protected)  

You've forgotten "abstract" and "static" (mind that we don't force the
order of those)

It saves 9 key strokes (while many IDEs can assist) and helps when
looking for issues in foreign code bases (what many of us do) so unless
there's anything new we should try to keep this list productive. 5.5 is
waiting to get out.

johannes

---

## #65994 — Peter Lind — 2013-02-19T21:03:04+00:00

URL: https://externals.io/message/65975#65994 · In reply to: #65990

> On Tue, 19 Feb 2013 19:10:22 -0000, Rasmus Lerdorf <rasmus@lerdorf.com>
> wrote:
>
> > […]
>
>
> As Sara noted, we shouldn't let users define methods without modifiers at
> all, so at least public/private/protected will have to be there.
>
>
> --
> 
> 
>

Just a quick check: you're absolutely certain this issue isn't much
better/easier handled with IDE macros/plugins? Something along the
lines of zen coding? If you're really that obsessed with typing 9
characters less, you must have considered keyboard shortcuts that let
you type the docblock, visibility, etc. in just 3-5 keystrokes.

Just a thought from userland
Peter

-- 
<hype>
WWW: plphp.dk / plind.dk
CV: careers.stackoverflow.com/peterlind
LinkedIn: plind
Twitter: kafe15
</hype

---

## #65996 — Sanford Whiteman — 2013-02-19T21:42:46+00:00

URL: https://externals.io/message/65975#65996 · In reply to: #65994

Seems this would complicate the transplanting of (global) functions
into (default public) class methods and vice versa. This is a common
refactoring task -- at least IME -- and before I adjust visibility I
would expect the function to Just Work.

So this works in a class to define the function:

function my_function() { }

And I expect to be able to pull that out into the global scope as-is.

But if people start using this super-shorthand in the class:

my_function() { }

Then when I pull it out into my function library, I'll get errors.

The reverse is also true: I expect to get a fatal from leaving off a
semicolon between function_call() and {} but at the top level of a
class it gets smoothly compiled as a function definition.

Look, I know there are similar cases throughout PHP (and other
languages) already, but why add more for no (IMO) payoff? Perhaps not
the most compelling case against this new sugar, but it would suffice
to stop me from ever using it.

-- Sandy

---

## #65975 — Nikita Nefedov — 2013-02-19T21:44:03+00:00

URL: https://externals.io/message/65975#65975

Hi!

As someone mentioned in the thread about short syntax for closures, we  
could also drop requirement for `function` keyword when defining/declaring  
methods in classes, interfaces or traits.

I have long noticed how redundant it is. The patch is pretty easy as it  
was with commas :)
It is absolutely backwards compatible (you can use `function` or you can  
not use it). Here's the patch: https://gist.github.com/nikita2206/4988075

If people will welcome this proposal, I would need some karma for making  
RFC.

---

## #65983 — Nikita Nefedov — 2013-02-19T22:39:07+00:00

URL: https://externals.io/message/65975#65983 · In reply to: #65979

On Tue, 19 Feb 2013 17:53:40 -0000, Johannes Schlüter  
<johannes@schlueters.de> wrote:

> I agreed to the conclusion that the "function" keyword provided a nice
> way to grep for functions when handling foreign code and leaving it out
> only provides little improvement in less typing.
>
> Please provide new arguments for a new discussion. (That thread was
> rather long)
>
> And as a general note I'd like to remind you all about Rasmus' recent
> mail: http://news.php.net/php.internals/65894 - In my words: Why not fix
> bugs instead of creating new ones?
>
> johannes
>
>

Hmm, I agree about grepping, but how often do you do it? Actually, last  
time I grepped php files was half a year ago I think, when I had just ssh  
connection and didn't want to mount sshfs. But usually there's IDEs that  
can statically analyze your code and let you search against huge codebases  
in seconds, I don't want to sound mean, but hey, does anybody greps php  
code?

What about reasoning - of course there's no reasons besides less typing.  
At the moment, usually when I want to define method, I usually already  
know will be in its body so I'm trying to type as fast as I can, and I  
often make some mistake in `public` keyword or `function`, actually it  
happens all the time :) So for me it would be pretty good feature if I  
could write one less word.

I agree with Sara about requirement for explicit declaration of visibility  
attr.

---

## #65998 — Zeev Suraski — 2013-02-19T23:01:55+00:00

URL: https://externals.io/message/65975#65998 · In reply to: #65975

Are we really trying to look under ground now for ways to change the
language syntax?

Unless there's a strong case to adding/removing/changing syntax, that
goes well beyond 'because we can' or 'it shortens the code' it
shouldn't even be brought up for discussion.

Zeev



> Hi!
>
> As someone mentioned in the thread about short syntax for closures, we could also drop requirement for `function` keyword when defining/declaring methods in classes, interfaces or traits.
>
> I have long noticed how redundant it is. The patch is pretty easy as it was with commas :)
> It is absolutely backwards compatible (you can use `function` or you can not use it). Here's the patch: https://gist.github.com/nikita2206/4988075
>
> If people will welcome this proposal, I would need some karma for making RFC.

---

## #65988 — Nikita Nefedov — 2013-02-19T23:07:51+00:00

URL: https://externals.io/message/65975#65988

On Tue, 19 Feb 2013 18:02:57 -0000, Christopher Jones  
<christopher.jones@oracle.com> wrote:
>
> What about including a few basic examples aka test cases in your patch?   
> If the feature is accepted, you'll need to include a LOT of testcases.
>
> Chris
>

Hi Chris,

this code could be a basic case (just for the sake of understanding)  
https://gist.github.com/nikita2206/4988665
But I will add an actual tests if it's going to receive some support.

On Tue, 19 Feb 2013 18:41:41 -0000, Rasmus Lerdorf <rasmus@lerdorf.com>  
wrote:

> 
>
> About 20-30 times every single day.
>
> -Rasmus

Hi Rasmus,

Are you grepping for all the functions or you are grepping just for some  
specific function? If so, you are likely already know what visibility this  
function has, so couldn't you grep for `public %functionName%` instead of  
`function %functionName%`?
At the end, you can always use `grep '(function|public|private|protected)  
functionName' file`, and if it's long to type, you can make sh script, or  
even alias.

---

## #65990 — Nikita Nefedov — 2013-02-19T23:12:31+00:00

URL: https://externals.io/message/65975#65990 · In reply to: #65989

On Tue, 19 Feb 2013 19:10:22 -0000, Rasmus Lerdorf <rasmus@lerdorf.com>  
wrote:

> 
>
> > […]
>
> public is the default visibility so it is often left off, so no, I can't
> grep for that.
>
> -Rasmus

As Sara noted, we shouldn't let users define methods without modifiers at  
all, so at least public/private/protected will have to be there.

---

## #66001 — Patrick ALLAERT — 2013-02-19T23:14:03+00:00

URL: https://externals.io/message/65975#66001 · In reply to: #65983

2013/2/19 Nikita Nefedov <inefedor@gmail.com>:
> Hmm, I agree about grepping, but how often do you do it? Actually, last time
> I grepped php files was half a year ago I think, when I had just ssh
> connection and didn't want to mount sshfs. But usually there's IDEs that can
> statically analyze your code and let you search against huge codebases in
> seconds, I don't want to sound mean, but hey, does anybody greps php code?

Yes, daily.

---

## #66006 — Levi Morrison — 2013-02-19T23:45:57+00:00

URL: https://externals.io/message/65975#66006 · In reply to: #65998

> Are we really trying to look under ground now for ways to change the
> language syntax?
>
> Unless there's a strong case to adding/removing/changing syntax, that
> goes well beyond 'because we can' or 'it shortens the code' it
> shouldn't even be brought up for discussion.

I am something of a language guru and to me syntax is *extremely*
important. Am I of the opinion that removing the function keyword from
the class definition will help? Yes, I am. Do I think it's some
incredible advancement? No, I don't.

The key question for me is: does removing it hurt PHP in any way? And
for me, the answer is a clear and resounding no. I do not grep code
for method definitions: I know exactly where they are and so will my
IDE if I happen to not be using vim. And for those who like to grep
for method definitions they can keep the keyword present.

---

## #66007 — Rasmus Lerdorf — 2013-02-19T23:49:49+00:00

URL: https://externals.io/message/65975#66007 · In reply to: #66006

> The key question for me is: does removing it hurt PHP in any way? And
> for me, the answer is a clear and resounding no. I do not grep code
> for method definitions: I know exactly where they are and so will my
> IDE if I happen to not be using vim. And for those who like to grep
> for method definitions they can keep the keyword present.

Except when I come along and try to grok your code. I rarely search my
own code.

From the very beginning of PHP we have always favoured readability over
fewer keystrokes.

-Rasmus

---

## #66009 — Sanford Whiteman — 2013-02-20T00:02:19+00:00

URL: https://externals.io/message/65975#66009 · In reply to: #66006

> I am something of a language guru and to me syntax is *extremely*
> important. Am I of the opinion that removing the function keyword from
> the class definition will help? Yes, I am. 

I'm missing the help. It saves 9 characters and creates a disjunction
between global function syntax and function-as-method syntax? Sorry, I
can't see this as anything better than neutral.

> And for those who like to grep for method definitions they can keep
> the keyword present.

... in their own code, not in all the code they are forced to maintain
(whether that be newbie sloppy code or super-cool library code).

-- S.

---

## #66010 — Levi Morrison — 2013-02-20T00:08:53+00:00

URL: https://externals.io/message/65975#66010 · In reply to: #66007

> > […]
>
> Except when I come along and try to grok your code. I rarely search my
> own code.

Protip: use an IDE.

No, I'm serious. I work from the command-line a lot. I grep, awk and
sed a lot of things on a weekly (sometimes daily) basis.  However, it
does not replace a good IDE.  Ever done a replace from the command
line only to find out you messed up some binary files and/or git/svn
information? Happens all the time when I don't use an IDE; good IDE's
don't make that mistake.

Let's stop pretending that the vast majority of PHP users actually
grep source code looking for `function foo`.  They don't. *They don't
even know how to use grep.* However, every PHP developer has to write
`function` and more importantly they have to read `function` over and
over again.

---

Does that mean that I think `function` ABSOLUTELY SHOULD BE made
optional? No. I don't.

Am I tired of everyone complaining about this particular issue and
pointing to the fact that some external tool won't work if you remove
the otherwise unnecessary keyword? Yes, I am. Take a step back and
look at your argument guys: you may have strong personal feelings
about it, but it's a pretty thin argument.

---

## #66011 — Herman Radtke — 2013-02-20T00:12:38+00:00

URL: https://externals.io/message/65975#66011 · In reply to: #66010

> Protip: use an IDE.

Linux is my IDE.

-- 
Herman Radtke
@hermanradtke | http://hermanradtke.com

---

## #66012 — Sanford Whiteman — 2013-02-20T00:24:44+00:00

URL: https://externals.io/message/65975#66012 · In reply to: #66010

> Let's stop pretending that the vast majority of PHP users actually
> grep source code looking for `function foo`.  They don't. *They don't
> even know how to use grep.* 

I don't grep as in `grep`, but as in "regex search that is part of my
IDE but is not also tokenizing/whateverizing/PHP-aware."

My IDE lets me search for the declaration of a method by
right-clicking class->method(), but it's only able to do that because
it knows how to find the class definition and look there (and it
doesn't use an autoloader). If I want to do a bulk search, it switches
to regex, which doesn't share that intelligence.

But I haven't upgraded to the v8 of this thing, only v7, so who
knows... I find it indispensable and I'm not going to switch to
another one because it isn't "smart enough" to do a PHP-aware bulk
search.

-- S.

---

## #66013 — johannes@schlueters.de — 2013-02-20T00:52:47+00:00

URL: https://externals.io/message/65975#66013 · In reply to: #66010

> Let's stop pretending that the vast majority of PHP users actually
> grep source code looking for `function foo`.

That was never said, but there's a big number of people who has to deal
with code produced by others and doing tasks where it is not always
appropriate to startup and IDE and create a project and index it and
"grep" is not always a simple grep but some more complex operation
involving a search.

And as you are happy with your idea you can certainly create shortcuts
so you can type "puf" or such instead of "public function" and maybe
even to hide the "function" word.

>   They don't. *They don't
> even know how to use grep.* However, every PHP developer has to write
> `function` and more importantly they have to read `function` over and
> over again.

The ones who don't know about 'grep' are happy about the extra clue.

> Am I tired of everyone complaining about this particular issue and
> pointing to the fact that some external tool won't work if you remove
> the otherwise unnecessary keyword? Yes, I am. Take a step back and
> look at your argument guys: you may have strong personal feelings
> about it, but it's a pretty thin argument.

The argument for removing (or rather: the argument for adding yet
another alternate syntax) is even weaker. Code is more often read than
written.

Now let's focus on work.

johannes
  (who will now ignore this thread - we're turning in circles)

---

## #66015 — Will Fitch — 2013-02-20T01:43:25+00:00

URL: https://externals.io/message/65975#66015 · In reply to: #65998

> Are we really trying to look under ground now for ways to change the
> language syntax?

Agree 100%. Not to mention, I plan on eventually convincing enough people to replace that keyword with a type hint ;)
> 
> Unless there's a strong case to adding/removing/changing syntax, that
> goes well beyond 'because we can' or 'it shortens the code' it
> shouldn't even be brought up for discussion.
> 
> Zeev
> 
> 
> 
> > […]

---

## #66022 — Sanford Whiteman — 2013-02-20T08:02:36+00:00

URL: https://externals.io/message/65975#66022 · In reply to: #66012

While I'm thinking about this (though I should leave it alone): who's
to say that PHP won't some day get inner classes? By deciding the
"default" inner member of a class will be a function, you're choosing
the one that has a global/procedural equivalent where the short syntax
won't work, instead of leaving the concept unused for the possible
future when:

class myClass {
        mySomething { // is equivalent to class mySomething {
        
(Yes, you could say mySomething(...) { } is a public function and
mySomething { } is an innner class, but you get the idea.)

-- S.

---

## #66024 — Nikita Nefedov — 2013-02-20T08:19:27+00:00

URL: https://externals.io/message/65975#66024 · In reply to: #65996

> Seems this would complicate the transplanting of (global) functions
> into (default public) class methods and vice versa. This is a common
> refactoring task -- at least IME -- and before I adjust visibility I
> would expect the function to Just Work.
> 
> So this works in a class to define the function:
> 
> function my_function() { }
> 
> And I expect to be able to pull that out into the global scope as-is.
> 
> But if people start using this super-shorthand in the class:
> 
> my_function() { }
> 
> Then when I pull it out into my function library, I'll get errors.
> 
> The reverse is also true: I expect to get a fatal from leaving off a
> semicolon between function_call() and {} but at the top level of a
> class it gets smoothly compiled as a function definition.
> 
> Look, I know there are similar cases throughout PHP (and other
> languages) already, but why add more for no (IMO) payoff? Perhaps not
> the most compelling case against this new sugar, but it would suffice
> to stop me from ever using it.

Hi Sandy, it won't complicate you anything.
Global functions remain its requirement for keyword, just methods lose it. And it doesn't mean that you can't write
class Foo {
function bar() {}
}

As I said, there are no bc breaks at all. My initial patch doesn't forces you to write any keyword, so you are actually can declare a method just with foo(){}, but it is absolutely doable, i'm just from my cell phone now.


What about readability - removal of this keyword will make a better readability of methods. Instead of
abstract class Foo {
public function make()
{
    // some code
}

public abstract function do();
}

You will need to read just
class Foo {
public make()
{
    // some code
}

public abstract do();
}

It's not function a keyword who lets you recognize function definition/declaration, but it's usually a pair of brackets. This function keyword is just noise for now, it doesn't serve any purpose aside from typing grep 'function funcname' instead of grep '(private|protected|public|abstract|static)funcname' , but, you know, if you love shell so much (or are forced to use it) you are probably familiar with creating 'shortcuts' for it, scripts or aliases.


Now what I think about all this "give the arguments" - the thing is - function keyword is just redundant. It's like appendix, we just don't need it, there's no point in it (yeah, except grepping), and we can drop it without any bc breaks, so why don't just do it?

---

## #66029 — Pierrick Charron — 2013-02-20T08:38:22+00:00

URL: https://externals.io/message/65975#66029 · In reply to: #66010

> Protip: use an IDE.
>

The IDE that i'm using may search for something like function \w to find
all the functions of my code. So I may have to wait for a new update of the
IDE to be able to use the index, and I also may have to pay to get the
update of my IDE. So why would I want all this if I can just keep the
function keyword.

Pierrick

---

Next page: https://externals.io/message/65975.md?page=2
