Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> utworzenie obiektu z klasy przekazanej w zmiennej
phoenix84
post
Post #1





Grupa: Zarejestrowani
Postów: 218
Pomógł: 0
Dołączył: 14.04.2004
Skąd: Słupsk, Nowy Sącz

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


Witam,

W zależności od tego co zostało wrzucone do funkcji, trzeba utworzyć obiekt innej klasy... czy jest na to inny sposób niż poza eval?

np.

  1. $klasa='xxx';
  2. $obiekt=new $klasa($parametr1, $parametr2);


pozdrawiam,

k.
Go to the top of the page
+Quote Post
CuteOne
post
Post #2





Grupa: Zarejestrowani
Postów: 2 958
Pomógł: 574
Dołączył: 23.09.2008
Skąd: wiesz, że tu jestem?

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


Np. używając ReflectionClass

  1. <?php
  2.  
  3. class A {
  4.  
  5. public function __construct() {
  6.  
  7. echo 'AAA <br />';
  8. }
  9.  
  10. public function b() {
  11.  
  12. echo 'BBB<br />';
  13. }
  14. }
  15.  
  16. function loadWithConstructor($sName) {
  17.  
  18. $ref = new ReflectionClass($sName);
  19. $class = $ref -> newInstance();
  20. $class -> b();
  21. }
  22.  
  23. function loadWithoutConstructor($sName) {
  24.  
  25. $ref = new ReflectionClass($sName);
  26. $inst = $ref->newInstanceWithoutConstructor();
  27. $inst -> b();
  28. }
  29.  
  30.  
  31. loadWithConstructor('A');
  32. loadWithoutConstructor('A');
Go to the top of the page
+Quote Post
phoenix84
post
Post #3





Grupa: Zarejestrowani
Postów: 218
Pomógł: 0
Dołączył: 14.04.2004
Skąd: Słupsk, Nowy Sącz

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


Cytat(CuteOne @ 13.07.2012, 20:10:17 ) *
Np. używając ReflectionClass

  1. <?php
  2.  
  3. class A {
  4.  
  5. public function __construct() {
  6.  
  7. echo 'AAA <br />';
  8. }
  9.  
  10. public function b() {
  11.  
  12. echo 'BBB<br />';
  13. }
  14. }
  15.  
  16. function loadWithConstructor($sName) {
  17.  
  18. $ref = new ReflectionClass($sName);
  19. $class = $ref -> newInstance();
  20. $class -> b();
  21. }
  22.  
  23. function loadWithoutConstructor($sName) {
  24.  
  25. $ref = new ReflectionClass($sName);
  26. $inst = $ref->newInstanceWithoutConstructor();
  27. $inst -> b();
  28. }
  29.  
  30.  
  31. loadWithConstructor('A');
  32. loadWithoutConstructor('A');


dobre (IMG:style_emoticons/default/smile.gif) , nie slyszalem o tym (IMG:style_emoticons/default/smile.gif) ... biore sie za testy (IMG:style_emoticons/default/smile.gif)

dzieki

k.

dziala super (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

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: 23.08.2025 - 04:16