Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> dynamiczne tworzenie instancji klasy wewnątrz innej klasy, __call, namespace
damian.1923
post
Post #1





Grupa: Zarejestrowani
Postów: 29
Pomógł: 0
Dołączył: 18.08.2008

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


witam

Proszę o podpowiedź jak prawidłowo utworzyć instancję klasy Child

  1. <?php
  2.  
  3. namespace app;
  4.  
  5. class One {
  6.  
  7. function __call($fname,$fargs) {
  8.  
  9. // sposób 1 :
  10.  
  11. $classname = ucfirst($fname);
  12. return new $classname;
  13.  
  14. // sposób 2 :
  15.  
  16. $a = eval("new " . ucfirst($fname) . "(" . implode(", ", $fargs) . ");");
  17. return $a;
  18. }
  19. }
  20.  
  21. class Child {
  22. function __construct() {
  23. echo "the child was born";
  24. }
  25. }
  26.  
  27.  
  28. $o = new One;
  29. $o->Child();
  30.  
  31. ?>


oba sposoby wyrzucają błąd:

Fatal error: Class 'Child' not found
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: 26.09.2025 - 00:56