note 96755 added to functions.anonymous
| From: | housni dot yakoob at gmail dot com | Date: | Sun, 14 Mar 2010 17:22:21 +0000 |
| Subject: | note 96755 added to functions.anonymous | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167015@lists.php.net to get a copy of this message | ||
If you want to make sure that one of the parameters of your function is a Closure, you can use Type
Hinting.
see: http://php.net/manual/en/language.oop5.typehinting.php
Example:
<?php
class TheRoot
{
public function poidh($param) {
echo "TheRoot $param!";
}
}
class Internet
{
# here, $my_closure must be of type object Closure
public function run_my_closure($bar, Closure $my_closure) {
$my_closure($bar);
}
}
$Internet = new Internet();
$Root = new TheRoot();
$Internet->run_my_closure($Root, function($Object) {
$Object->poidh(42);
});
?>
The above code simply yields:
"TheRoot 42!"
NOTE: If you are using namespaces, make sure you give a fully qualified namespace.
print_r() of Internet::run_my_closure's $my_closure
<?php
Closure Object
(
[parameter] => Array
(
[$Object] =>
)
)
?>
var_dump() of Internet::run_my_closure's $my_closure
<?php
object(Closure)#3 (1) {
["parameter"]=>
array(1) {
["$Object"]=>
string(10) ""
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 112.135.166.126
----
Manual Page -- http://www.php.net/manual/en/functions.anonymous.php
Edit -- https://master.php.net/note/edit/96755
Del: integrated -- https://master.php.net/note/delete/96755/integrated
Del: useless -- https://master.php.net/note/delete/96755/useless
Del: bad code -- https://master.php.net/note/delete/96755/bad+code
Del: spam -- https://master.php.net/note/delete/96755/spam
Del: non-english -- https://master.php.net/note/delete/96755/non-english
Del: in docs -- https://master.php.net/note/delete/96755/in+docs
Del: other reasons-- https://master.php.net/note/delete/96755
Reject -- https://master.php.net/note/reject/96755
Search -- https://master.php.net/manage/user-notes.php