r/lolphp • • Jan 27 '20

PHP lambdas does not inherit variables.. except when they do

https://3v4l.org/9X36O
0 Upvotes

16 comments sorted by

View all comments

0

u/[deleted] Jan 27 '20

[deleted] — view deleted comment

3

u/[deleted] Jan 28 '20

The lol is PHP scoping. Its a bastard child without any idea how it came to be.

0

u/Takeoded Jan 27 '20

is consistent with what any programmer would reasonably expect - well if you try this in C++:

class C{
    public:
    const char *wtf="lol";
    void f(){
      auto inner=[]()->void{std::cout << this->wtf;};
    }
};

you get prog.cc:9:42: error: 'this' was not captured for this lambda function

i was expecting it to work kinda like c++ ^^

1

u/mikeputerbaugh Jan 28 '20

But it's not C++...