Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Singleton]Lista argumentów do przekazania
daniel1302
post
Post #1





Grupa: Zarejestrowani
Postów: 602
Pomógł: 30
Dołączył: 1.08.2007
Skąd: Nowy Sącz

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


Witam, mam problem, jeśli w nagłówku 1 funkcji podaję 5 argumentów(Singleton::instance('KlasaTest', $arg1, $arg2, $arg3, $arg4, $arg5)(IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg) to jak mam przekazać te 5 argumentów($arg[1-5]) do instancji klasy new $class ($args).

Próbowałem zrobić tak
  1. <?php
  2. function foo2($arg1, $arg2, $arg3)
  3. {
  4.    echo $arg1.':'.$arg2.':'.$arg3;
  5. }
  6.  
  7. function foo()
  8. {
  9.    $arg_list = func_get_args();
  10.    
  11.    if (isset($arg_list[0]))
  12.        $arg_list2 = array_shift($arg_list);
  13.        
  14.    if (isset($arg_list[1]))
  15.        foreach ($arg_list as $k) {
  16.            $arg_list2 .= ', '.$k;
  17.        }
  18.        
  19.    foo2 ($arg_list2);
  20. }
  21.  
  22. foo(1, 2, 3);
  23. ?>
Go to the top of the page
+Quote Post
wookieb
post
Post #2





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




http://pl2.php.net/manual/pl/function.call...-func-array.php
Go to the top of the page
+Quote Post
daniel1302
post
Post #3





Grupa: Zarejestrowani
Postów: 602
Pomógł: 30
Dołączył: 1.08.2007
Skąd: Nowy Sącz

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


Właśnie znalazłem
podaje rozwiązanie
  1. <?php
  2. function foo2($arg1, $arg2, $arg3)
  3. {
  4.    echo $arg1.':'.$arg2.':'.$arg3;
  5. }
  6.  
  7. function foo()
  8. {
  9.    $arg_list = func_get_args();
  10.    
  11.    call_user_func_array('foo2', $arg_list);
  12. }
  13.  
  14. foo(1, 2, 3);
  15. ?>

Kurde po 5 godzinach wpadło


Sorry wookieb masz + właśnie przeglądałem manuala na temat callback i wpadło

Mogę wiedzieć jeszcze jak zastosować to na klasach.
Nigdzie niema opisane a nie mogę sam znaleźć rozwiązania

Ten post edytował daniel1302 29.05.2009, 19:10:13
Go to the top of the page
+Quote Post
wookieb
post
Post #4





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




Jest kolego tylko nieuwaznie czytasz
  1. <?php
  2. call_user_func(array('nazwa_klasy', 'metoda_statyczna'), $argumenty); // == nazwa_klasy::metoda_statyczna($argumenty)
  3.  
  4. call_user_funct(array($egzemplarz, 'metoda'), $argumenty); // == $egzemplarz->metoda($argumenty);
  5. ?>
Go to the top of the page
+Quote Post
daniel1302
post
Post #5





Grupa: Zarejestrowani
Postów: 602
Pomógł: 30
Dołączył: 1.08.2007
Skąd: Nowy Sącz

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


Niema przynajmniej ja nie widzę. Dzięki jeszcze raz
Go to the top of the page
+Quote Post
wookieb
post
Post #6





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




http://pl2.php.net/callback Rzuca sie w oczy jak... facet do kobiety (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
lesiuk
post
Post #7





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 22.08.2008

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


Właśnie miałem o to pytać, a jak to wykonać na nie statycznym konstruktorze?

Próbowałem tak
call_user_func_array(new Test, $args);
call_user_func_array(array($testInstance', ''), $args);
call_user_func_array(array('Test', '__construct'), $args);

W tym ostatnim przypadku wyskakuje, że konstruktor nie może być ujemny
Go to the top of the page
+Quote Post
wookieb
post
Post #8





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




http://pl.php.net/manual/pl/language.oop5.basic.php
Przykład 5
Go to the top of the page
+Quote Post
lesiuk
post
Post #9





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 22.08.2008

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


Ale proszę o pomoc jak to wykorzystać, bo jak w dam poza call_user_func_array() new XXX
to zwraca errory bo konstruktor wykonuje się bez wartości

Ten post edytował lesiuk 29.05.2009, 20:15:40
Go to the top of the page
+Quote Post
wookieb
post
Post #10





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




Pokaz kod bo troche dziwnie piszesz.

Ten post edytował wookieb 29.05.2009, 20:20:04
Go to the top of the page
+Quote Post
lesiuk
post
Post #11





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 22.08.2008

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


  1. <?php
  2. Class Test
  3. {
  4.    function __construct($arg1, $arg2, $arg3)
  5.    {
  6.        echo $arg1.':'.$arg2.':'.$arg3;
  7.    }
  8. }
  9.  
  10. function foo(array $arg = null)
  11. {    
  12.    $class = new Test();
  13.    call_user_func_array($class, $arg);
  14. }
  15.  
  16. foo(array(1,2,3));
  17. ?>



A to błędy

Kod
[b]Warning[/b]:  Missing argument 1 for Test::__construct(), called in D:\Program Files\WebServ\httpd\test.php on line 19 and defined in [b]D:\Program Files\WebServ\httpd\test.php[/b] on line [b]10[/b]

[b]Warning[/b]:  Missing argument 2 for Test::__construct(), called in D:\Program Files\WebServ\httpd\test.php on line 19 and defined in [b]D:\Program Files\WebServ\httpd\test.php[/b] on line [b]10[/b]

[b]Warning[/b]:  Missing argument 3 for Test::__construct(), called in D:\Program Files\WebServ\httpd\test.php on line 19 and defined in [b]D:\Program Files\WebServ\httpd\test.php[/b] on line [b]10[/b]

[b]Notice[/b]:  Undefined variable: arg1 in [b]D:\Program Files\WebServ\httpd\test.php[/b] on line [b]12[/b]

[b]Notice[/b]:  Undefined variable: arg2 in [b]D:\Program Files\WebServ\httpd\test.php[/b] on line [b]12[/b]

[b]Notice[/b]:  Undefined variable: arg3 in [b]D:\Program Files\WebServ\httpd\test.php[/b] on line [b]12[/b]
::
[b]Catchable fatal error[/b]:  Object of class Test could not be converted to string in [b]D:\Program Files\WebServ\httpd\test.php[/b] on line [b]19[/b]
Go to the top of the page
+Quote Post
wookieb
post
Post #12





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




  1. <?php
  2. Class Test
  3. {
  4.   function __construct($arg1, $arg2, $arg3)
  5.   {
  6.       echo $arg1.':'.$arg2.':'.$arg3;
  7.   }
  8. }
  9.  
  10.  
  11.  
  12. $cl= new ReflectionClass('Test');
  13. print_r($cl);
  14.  
  15. $cl2=$cl->newInstanceArgs(array('hehe', 'lol', 'ble'));
  16. print_r($cl2);
  17. ?>


Ten post edytował wookieb 29.05.2009, 20:51:29
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 Aktualny czas: 12.10.2025 - 13:12