Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [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
 
Start new topic
Odpowiedzi
lesiuk
post
Post #2





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

Posty w temacie


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

 



RSS Aktualny czas: 14.10.2025 - 06:12