Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Tymczasowy obiekt
Forum PHP.pl > Forum > PHP > Object-oriented programming
michalos13
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. ?>
wookieb
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.
michalos13
Dobrze mnie zrozumiałeś winksmiley.jpg Ale klasa (text) u mnie wyglądało mniej-więcej tak:

  1. <?php
  2. class text
  3. {
  4.    private $txt;
  5.    public function createElement($conf)
  6.    {
  7.        $this->txt = $this->txt . '123';
  8.        $this->txt = $this->txt . 'abc';
  9.        return $this->txt;
  10.    }
  11. }
  12. ?>


Ale kiedy zrobię tak jak w twoim przykładzie (zwracam od razu tekst) to działa.
wookieb
Więc już chyba wszystko ok?
A jeżeli nie ok to rozumiem, że to nie jest dokładny kod i wtedy musisz raczej pokazać ten, który powoduje problemy smile.gif
michalos13
No więc:

  1. <?php
  2. class checkbox implements phpfElement
  3. {
  4.    private $fullElement;
  5.  
  6.    function createElement( $element )
  7.    {
  8.        require_once( 'namespace.php' );
  9.        if( isset( $element[ $namespace[ 'name' ] ] ) )
  10.        {
  11.            $this->elementAdd( '<input type="checkbox" name="' . $element[ $namespace[ 'name' ] ] . '" ' );
  12.            if( isset( $element[ $namespace[ 'value' ] ] ) )
  13.            {
  14.                $this->elementAdd( 'value="' . $element[ $namespace[ 'value' ] ] . '" ' );
  15.            }
  16.            if( isset( $element[ $namespace[ 'size' ] ] ) )
  17.            {
  18.                $this->elementAdd( 'size="' . $element[ $namespace[ 'size' ] ] . '" ' );
  19.            }
  20.            if( isset( $element[ $namespace[ 'maxlength' ] ] ) )
  21.            {
  22.                $this->elementAdd( 'maxlength="' . $element[ $namespace[ 'maxlength' ] ] . '" ' );
  23.            }
  24.            if( $element[ $namespace[ 'readonly' ] ] )
  25.            {
  26.                $this->elementAdd( 'readonly="readonly" ' );
  27.            }
  28.            if( $element[ $namespace[ 'disabled' ] ] )
  29.            {
  30.                $this->elementAdd( 'disabled="disabled" ' );
  31.            }
  32.            if( isset( $element[ $namespace[ 'tabindex' ] ] ) )
  33.            {
  34.                $this->elementAdd( 'tabindex="' . $element[ $namespace[ 'tabindex' ] ] . ' ' );
  35.            }
  36.            if( isset( $element[ $namespace[ 'class' ] ] ) )
  37.            {
  38.                $this->elementAdd( 'class="' . $element[ $namespace[ 'class' ] ] . '" ' );
  39.            }
  40.            if( isset( $element[ $namespace[ 'id' ] ] ) )
  41.            {
  42.                $this->elementAdd( 'id="' . $element[ $namespace[ 'id' ] ] . '" ' );
  43.            }
  44.            if( $element[ $namespace[ 'checked' ] ] )
  45.            {
  46.                $this->elementAdd( 'checked="checked" ' );
  47.            }
  48.            $this->elementAdd( ' />');
  49.            
  50.            
  51.            if( isset( $element[ $namespace[ 'label' ] ] ) )
  52.            {
  53.                $this->elementAdd( '<label for="' . $element[ $namespace[ 'id' ] ] . '">'
  54.                                   . $element[ $namespace[ 'label' ] ] . '</label>'
  55.                                 );
  56.            }
  57.        }
  58.        return $this->fullElement;
  59.    }
  60.    
  61.    private function elementAdd( $string )
  62.    {
  63.        $this->fullElement = $this->fullElement . $string;
  64.    }
  65. }
  66. ?>


i namespace.php

  1. <?php
  2. $namespace[ 'name' ] = 'name';
  3. $namespace[ 'text' ] = 'text';
  4. $namespace[ 'value' ] = 'value';
  5. $namespace[ 'size' ] = 'size';
  6. $namespace[ 'maxlength' ] = 'maxlength';
  7. $namespace[ 'readonly' ] = 'readonly';
  8. $namespace[ 'disabled' ] = 'disabled';
  9. $namespace[ 'input' ] = 'input';
  10. $namespace[ 'class' ] = 'class';
  11. $namespace[ 'label' ] = 'label';
  12. $namespace[ 'id' ] = 'id';
  13. $namespace[ 'multiple' ] = 'multiple';
  14. $namespace[ 'optgroup' ] = 'optgroup';
  15. ?>
wookieb
Problem polega na tym ze użyłeś require_once, który działa tak, że jeżeli raz wcześniej dołączono plik do skryptu to drugi raz tego nie zrobi. Użyj samego require.
michalos13
Wiedziałem że to moja głupota ;-p Dzięki.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.