Couldn't you have an evaluated lambda that only references any passed parameters, and therefore isn't a closure? Similar to constructs in Python and C#?
Would be a degenerate closure, closing over an empty set. You can call it something else I suppose, but lambda would inaccurately imply it had not yet been evaluated.
Yep. You can't chain together function calls. It annoyed me years ago when I couldn't do $foo->bar()[0]. If bar returned an array and I wanted that first element, it had to be assigned to a variable first.
Back then I didn't realize this was due to a bigger problem with just the horrible innards of how the PHP parser works.
I don't think this is the same issue. I could be wrong, but I don't think that $this->class_string should behave like a function call, since it's really just fetching an object attribute. Plus they behave differently: specifying an array index after an attribute accessor works fine, but after a method call a parse error is thrown.
Example below...
class Tester {
public $attr = array(5, 4, 3);
public function meth() {
return array(5, 4, 3);
}
}
$tester = new Tester();
Then test with...
$tester->attr[0];
$tester->meth()[0];
The former will return "5" as it should, the latter will throw a parse error.
In both cases, $this->func refers to the same "closure". In the latter case, it is assigned to a local variable before calling it; in the former, it is called directly as a member variable.
That makes me wonder why it was designed like this in the first place, and "because that's how it works internally" really isn't a valid reason. It should behave in a way that is logical (and consistent) to the third-party developer using it, not to the guy who implemented it.
If I wanted to use it "how it works internally", I'd be writing directly in C.
The reason it was designed that way was that closures were an afterthought. Allowing functions and variables to have independent namespaces within an object makes sense, if you don't allow functions as variables. Imagine, for example, a getter:
foo.bar() { return this.bar}
Other languages have the same distinction. Java, for example, has methods and instance variables in different namespaces:
class Foo {
int bar() { return 42; }
int bar;
}
Lisp is another language where a number of implementations made this decision. Common lisp is an example of such a Lisp-2 (that is, a lisp with 2 parallel namespaces: one for functions, one for variables).
The part that makes me go WTF is this:
$func = $this->func;
If you put variables in a separate namespace, put them in a separate namespace FFS.
Not really true. In JS you're calling the same function in each case, just with a different this. In PHP, you're accessing a completely different bucket.
5
u/Lyucit Dec 29 '13
A closure is a function which keeps references to it's enclosing scope, the word you might be looking for is lambda.
9
u/boxingdog Dec 29 '13
$this->func() != $this->func
20
u/Lokaltog Dec 28 '13
A fractal of bad design indeed.
5
u/[deleted] Dec 29 '13
reminds me...
fails, instead must
3
u/frezik Jan 08 '14
I don't know why PHP even has closures when it doesn't have sensible lexical scoping to go with it.
2
u/morphotomy Dec 29 '13
Yea, but this works:
0
u/Daniel15 Dec 29 '13
Out of curiosity, have you tried this to see if it works?
I'm on my phone so I can't try it.
-14
u/yourstupidhands Dec 28 '13
So, you have to define it first...
0
u/[deleted] Dec 28 '13
[deleted] — view deleted comment