PHP ARRAY FUNCTIONS, FUNCTION AS ARRAY VALUE

I can"t seem to lớn find anything of this, & was wondering if it"s possible lớn store a function or function reference as a value for an array element. For e.g.

array("someFunc" => &x(), "anotherFunc" => $this->anotherFunc())Thanks!


*

*

Introducing: Trending sort You can now choose to lớn sort by Trending, which boosts votes that have happened recently, helping lớn surface more up-to-date answers.

Bạn đang xem: Php array functions, function as array value

Trending is based off of the highest score sort & falls back lớn it if no posts are trending.

Try it Dismiss
Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
You can "reference" any function. A function reference is not a reference in the sense of "address in memory" or something. It"s merely the name of the function.

"strlen", "class_function" => array("ClassName", "functionName"), "object_method" => array($object, "methodName"), "closure" => function($foo) return $foo; ,);// while this works$functions<"regular">();// this doesn"t$functions<"class_function">();// to lớn make this work across the board, you"ll need eithercall_user_func($functions<"object_method">, $arg1, $arg2, $arg3);// orcall_user_func_array($functions<"object_method">, array($arg1, $arg2, $arg3));

*

PHP supports the concept of variable functions, so you can bởi vì something like this:

function foo() echo "bar"; $array = array("fun" => "foo");$array<"fun">();Yout can check more examples in manual.


*

Yes, you can:

$array = array( "func" => function($var) return $var * 2; ,);var_dump($array<"func">(2));This does, of course, require PHP anonymous function support, which arrived with PHP version 5.3.0. This is going to leave you with quite unreadable code though.

Xem thêm: Liên Minh Huyền Thoại Mobile Apk V3, League Of Legends: Wild Rift Apk For Android


*

check out PHP"s call_user_func. Consider the below example.

consider two functions

function a($param) return $param;function b($param) return $param;$array = array("a" => "first function param", "b" => "second function param");now if you want lớn execute all the function in a sequence you can bởi vì it with a loop.

foreach($array as $functionName => $param) call_user_func($functioName, $param);plus array can hold any data type, be it function call, nested arrays, object, string, integer etc. Etc.


Thanks for contributing an answer to Stack Overflow!

Please be sure khổng lồ answer the question. Provide details & share your research!

But avoid

Asking for help, clarification, or responding lớn other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.


Post Your Answer Discard

By clicking “Post Your Answer”, you agree to lớn our terms of service, privacy policy and cookie policy


Not the answer you're looking for? Browse other questions tagged php arrays function function-pointers or ask your own question.


Site thiết kế / biểu tượng logo © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. Rev2022.7.1.42502


Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.