G
G
Grigory Vasilkov2017-01-16 09:40:38
PHP
Grigory Vasilkov, 2017-01-16 09:40:38

Why doesn't PHP code with closures work?

Is this due to a feature of PHP, or maybe StdClass just doesn't allow you to do such tricks? Or just 5.6 does not know how? (ps. I try to write in js notation)

$obj = new StdClass();
  $obj->fn = new StdClass();
  $obj->fn->move = function ($a, $b) {
    print_r($this);
    return $a * $b;
  };
  print_r($obj->fn->move(1,2));

Fatal error: Call to undefined method stdClass::move() in [file] on line [line]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri, 2017-01-16
@gzhegow

StdClass cannot add methods.
When I see code like this, I'm really shaking.
this is not js for you, here you can create normal human named classes.
UPD: only for sports purposes I will show how it can work

$f = $obj->fn->move;
  print_r($f(1,2));

that is, the function is still preserved. but the new method is not added to the class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question