Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Tymczasowy obiekt
michalos13
post
Post #1





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 2.06.2009

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


Ostatnio zmagam się z problemem którego nie mogę rozwiązać. Tworzę klasę która doda do tablicy wartość wygenerowaną z innej klasy. Robię w funkcji "tymczasowy obiekt" (nie wiem jak to nazwać) klasy, wywołuję z niej funkcję która zwraca wartość i zostaje zapisana ona do tablicy. Potem pobieram z tablicy interesującą mnie wartość. Za pierwszym razem zwracana wartość jest dobra ale potem wartość == null.

  1. <?php
  2. class Klasa
  3. {
  4.    private $controls = array();
  5.    
  6.    public function addElement( $id , $name , $config )
  7.    {
  8.        $reflect = new ReflectionClass( $name );
  9.        $element = $reflect->newInstance();
  10.        $this->controls[$id] = $element->createElement( $config  );
  11.    }
  12.    
  13.    public function getElement( $id )
  14.    {
  15.        return $this->controls[$id] ;
  16.    }
  17. }
  18.  
  19. $a = new Klasa();
  20. $a->addElement( 'id' , 'text' , array( 'cos' => 'costam' ) );
  21.  
  22. echo $a->getElement( 'id' );
  23. ?>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
wookieb
post
Post #2





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




Pokaż proszę kod klasy text u mnie coś takiego działa
  1. <?php
  2. class text
  3. {
  4.    public function createElement($conf)
  5.    {
  6.        return 'hehe';
  7.    }
  8. }
  9.  
  10. class Klasa
  11. {
  12.   private $controls = array();
  13.  
  14.   public function addElement( $id , $name , $config )
  15.   {
  16.       $reflect = new ReflectionClass( $name );
  17.       $element = $reflect->newInstance();
  18.       $this->controls[$id] = $element->createElement( $config  );
  19.   }
  20.  
  21.   public function getElement( $id )
  22.   {
  23.       return $this->controls[$id] ;
  24.   }
  25. }
  26.  
  27. $a = new Klasa();
  28. $a->addElement( 'id' , 'text' , array( 'cos' => 'costam' ) );
  29.  
  30. echo $a->getElement( 'id' );
  31. echo $a->getElement( 'id' );
  32. ?>

Ale nie wiem czy do końca dobrze zrozumiałem.
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: 6.10.2025 - 14:38