note 117260 added to language.types.callable

From: Date: Mon, 11 May 2015 13:36:17 +0000
Subject: note 117260 added to language.types.callable
Groups: php.notes 
Request: Send a blank email to php-notes+get-202498@lists.php.net to get a copy of this message
A note on differences when calling callbacks as "variable functions" without the use of call_user_func() (e.g. "<?php $callback = 'printf'; $callback('Hello World!') ?>"): - Using the name of a function as string has worked since at least 4.3.0 - Calling anonymous functions and invokable objects has worked since 5.3.0 - Using the array structure [$object, 'method'] has worked since 5.4.0 Note, however, that the following are not supported when calling callbacks as variable functions, even though they are supported by call_user_func(): - Calling static class methods via strings such as 'foo::doStuff' - Calling parent method using the [$object, 'parent::method'] array structure All of these cases are correctly recognized as callbacks by the 'callable' type hint, however. Thus, the following code will produce an error "Fatal error: Call to undefined function foo::doStuff() in /tmp/code.php on line 4": <?php class foo { static function callIt(callable $callback) { $callback(); } static function doStuff() { echo "Hello World!"; } } foo::callIt('foo::doStuff'); ?> The code would work fine, if we replaced the '$callback()' with 'call_user_func($callback)' or if we used the array ['foo', 'doStuff'] as the callback instead. ---- Server IP: 72.52.91.14 Probable Submitter: 128.214.190.20 ---- Manual Page -- http://php.net/manual/en/language.types.callable.php Edit -- https://master.php.net/note/edit/117260 Del: integrated -- https://master.php.net/note/delete/117260/integrated Del: useless -- https://master.php.net/note/delete/117260/useless Del: bad code -- https://master.php.net/note/delete/117260/bad+code Del: spam -- https://master.php.net/note/delete/117260/spam Del: non-english -- https://master.php.net/note/delete/117260/non-english Del: in docs -- https://master.php.net/note/delete/117260/in+docs Del: other reasons-- https://master.php.net/note/delete/117260 Reject -- https://master.php.net/note/reject/117260 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202498) next »