Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]funkcja która inną funkcję jako parametr
codex
post 9.08.2017, 21:17:58
Post #1





Grupa: Zarejestrowani
Postów: 35
Pomógł: 0
Dołączył: 29.11.2015

Ostrzeżenie: (0%)
-----


witam. powie ktoś jak mam napisać funkcję, która jako parametr przyjmuje adres innej funkcji. wszystko fajnie wychodzi w momencie gdy funkcja będąca parametrem jest bez argumentów - tu nie ma specjalnie filozofii. problem pojawia się wtedy gdy ta funkcja której adres ma być parametrem, ma przyjmować też argumenty - jak bym tego nie zapisał to wywala mi błędy :

  1. // funkcja której adres zostanie przekazany jako parametr:
  2. function service( $argx ) {
  3. echo '<br> service = '.$argx;
  4. }
  5.  
  6. // funkcja która ma przyjąć adres innej funkcji jako jeden z parametrów:
  7. function tester( $argx, $service ) {
  8. $service( $argx );
  9. return true;
  10. }
  11.  
  12. // wywołanie próbne:
  13. tester( 'message', service($x) );

jakoś w sieci nie widzę przykładów dla php ....
Go to the top of the page
+Quote Post
viking
post 9.08.2017, 21:30:35
Post #2





Grupa: Zarejestrowani
Postów: 6 378
Pomógł: 1116
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Funkcja service nic nie zwraca więc w tester ma wartość null.


--------------------
Go to the top of the page
+Quote Post
vokiel
post 9.08.2017, 21:33:30
Post #3





Grupa: Zarejestrowani
Postów: 2 592
Pomógł: 445
Dołączył: 12.03.2007

Ostrzeżenie: (0%)
-----


  1. function service( ) {
  2. return "Service args: ".print_r(func_get_args(), true).PHP_EOL;
  3. }
  4.  
  5. function tester( $service ) {
  6. $args = func_get_args();
  7. unset($args[0]); // $service
  8. return call_user_func_array($service, $args );
  9. }
  10.  
  11. echo tester( 'service', 'arg-1' );
  12. /*
  13. Service args: Array
  14. (
  15.   [0] => arg-1
  16. )
  17. */
  18.  
  19. echo tester( 'service', 'arg-1', 'arg-2' );
  20. /*
  21. Service args: Array
  22. (
  23.   [0] => arg-1
  24.   [1] => arg-2
  25. )
  26.  
  27. */


--------------------
Go to the top of the page
+Quote Post
codex
post 9.08.2017, 21:44:31
Post #4





Grupa: Zarejestrowani
Postów: 35
Pomógł: 0
Dołączył: 29.11.2015

Ostrzeżenie: (0%)
-----


gitara, działa. tyle się z tym męczyłem ... a tutaj w 10 minet problem rozwiązany. dzięki vokiel.
Go to the top of the page
+Quote Post
trueblue
post 9.08.2017, 21:47:43
Post #5





Grupa: Zarejestrowani
Postów: 6 804
Pomógł: 1827
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


  1. function service( $argx ) {
  2. echo '<br> service = '.$argx;
  3. }
  4.  
  5. // funkcja która ma przyjąć adres innej funkcji jako jeden z parametrów:
  6. function tester( $service, $serviceArgs ) {
  7. call_user_func_array($service, $serviceArgs );
  8. return true;
  9. }
  10.  
  11. // wywołanie próbne:
  12. $x='abc';
  13. tester( 'service', array($x) );


--------------------
Go to the top of the page
+Quote Post
viking
post 10.08.2017, 06:13:25
Post #6





Grupa: Zarejestrowani
Postów: 6 378
Pomógł: 1116
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Myślałem początkowo że service ma zwrócić nazwę funkcji która ma się wykonać. To dużo szybsze (zwłaszcza od php7) i bardziej czytelne, jest bezpośrednie wywołanie:
  1. function service( $argx ) {
  2. var_export($argx);
  3. }
  4.  
  5. function tester( $service, ...$serviceArgs ) {
  6. if (is_callable($service)) {
  7. $service( $serviceArgs );
  8. return true;
  9. }
  10. return false;
  11. }
  12. tester( 'service', 1,2,['a','b'] );


Ten post edytował viking 10.08.2017, 06:17:13


--------------------
Go to the top of the page
+Quote Post
codex
post 15.08.2017, 16:44:21
Post #7





Grupa: Zarejestrowani
Postów: 35
Pomógł: 0
Dołączył: 29.11.2015

Ostrzeżenie: (0%)
-----


Noooo Viking ok faktycznie pomogłeś nawet bardziej te 3 kropki dla tablicy konkretny zapis bardzo to czytelne dzięki. Ja łamaną angielszczyzną ten manual php gwałciłem i kombinowałem z tym is_callable - tłumacząc dosłownie "jest wywoływalny" nie zajażyłem że to funkcja która testuje parametr smile.gif ..... Ok dzięki pomogliście panowie zamykam rozdział.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 29.06.2025 - 10:19