Skip to content

Instantly share code, notes, and snippets.

@jankuca
Created November 1, 2012 21:29
Show Gist options
  • Save jankuca/3996709 to your computer and use it in GitHub Desktop.
Save jankuca/3996709 to your computer and use it in GitHub Desktop.
$a = function () {
echo 'x';
};
$a();
$x = 4;
$a = function () {
echo $x; // x is not defined
};
$a();
$x = 4;
$b = function () use ($x) {
echo $x;
};
$b();
$a = new stdClass();
$a->b = function () {
echo 'x';
};
$a->b(); // no such method 'b'
$a = new stdClass();
$a->b = function () {
echo 'x';
};
call_user_func($a->b); // no such method 'b'
class A {
public function b() {
return function () {
$this->c(); // this cannot be used inside a closure
};
}
public function c() {
};
}
$a = new A();
$a->b();
class A {
public function b() {
$self = $this;
return function () use ($self) {
$self->c();
$self->d(); // call to a protected class from outside the object
};
}
public function c() {
};
protected function d() {
};
}
$a = new A();
$a->b();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment