0

Let's assume we have this function

<?php
function run($callback)
{
    $callback($some, $params);
}

How to call it?

run($someObject->callback);

or rather

run([$someObject, 'callback']);

The first one seems better to me especially because of code suggestion but in documentation is used array notation.

Why is first one worse than second one?

2
  • 2
    Does the first one even work? It would only work if "callback" was a variable whose value was a function. Commented Jun 29, 2017 at 8:21
  • Use any of the methods described in the documentation of PHP Callables. Commented Jun 29, 2017 at 8:23

1 Answer 1

1

The array notation is better because the arrow notation doesn't work. Functions in PHP aren't first class objects which can be passed around. Whenever you "pass a function", you must pass it by name; i.e. only its name. To pass an object method, you need to pass the object and the method name using the callable pseudo type notation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.