A
A
Alexander2017-11-10 17:41:50
MySQL
Alexander, 2017-11-10 17:41:50

Type hint. Returns not what is expected. Where is the mistake?

In the example: echo foo($object, "func"); should return - MyClass::func, but returns MyClass::__invoke.
Thank you.

class MyClass{
  function func(){
    return __METHOD__;
  }
  static function staticMethod(){
    return __METHOD__;
  }
  function __invoke(){
    return __METHOD__;
  }
}
$object = new MyClass();

function foo(callable $x){
  return $x();
}

echo foo($object, "func"); // MyClass::func (возвращает MyClass::__invoke)
echo foo(["MyClass", "staticMethod"]); // MyClass::staticMethod
echo foo($object); // MyClass::__invoke

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Exteris, 2019-08-19
@LamerFromSpace

Having limits the already prepared data selection. Yours doesn't have a table_schema column.
Change to where.

A
Artray, 2017-11-10
@storm-coder

echo foo([$object, "func"]); // MyClass::func

K
Kirill Nesmeyanov, 2017-11-10
@SerafimArts

$object contains __invoke , which means it is callable . The foo function just calls it.
We get: Same as:
$object(); // Вызов метода __invoke

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question