Ankündigung

Einklappen
Keine Ankündigung bisher.

Generische Datentypen in PHP

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Generische Datentypen in PHP

    Hallo zusammen,

    ich bin neu in diesem Forum, entwickle aber schon seit einigen Jahren Software. Zuerst in C++ und seit 3 Jahre nun auch in PHP.

    In unserer Firma sind wir in der OOP-Welt zuhause und versuchen dort auch zu bleiben was uns meist gelingt. Außerdem halten wir uns so gut es geht an die strikte Typisierung in unserem Code. Z.B. nutzen wir die PHP-Arrays in den meisten Fällen nicht direkt sondern schreiben uns Collections für JEDEN Datentype der außerhalb einer Klasse in Listen verwaltet werden muss.
    Das hat mich schnell dazu geführt mir Gedanken darüber zu machen, warum es wohl in PHP keine Generischen Datentypen gibt wie z.B. in C++. Mit einem solchen generischen Typ wäre es einfach einen Typ
    Code:
    class Liste<class T>
    für viele verschiedene Datentypen zu erstellen und trotzdem Typ-sicher zu arbeiten.

    Auf der Seite https://wiki.php.net/rfc/generics gibt es bereits einen Vorschlag dazu der bisher nicht mit in die Standards aufgenommen wurde und es wohl auch in Zukunft nicht so schnell da rein schaffen wird, da es noch immer ein Entwurf ist.

    Mich würde es interessieren was eure Meinung dazu ist.

  • #2
    https://github.com/ircmaxell/PhpGenerics

    Hrhrhr.

    Meine Meinung: wenn du ordentliche Typisierung willst, dann nutze nicht PHP. Generics würde ich aber dennoch befürworten. Für die statische Code-Analyse (damit PhpStorm meckern kann) kriegt man das allerdings auch anders hin:

    PHP-Code:
    /**
     * Class GenericCollection
     */
    class GenericCollection implements \IteratorAggregate, \ArrayAccess{
        
    /**
         * @var string
         */
        
    private $type;

        
    /**
         * @var array
         */
        
    private $items = [];

        
    /**
         * @param string $type
         */
        
    public function __construct(string $type){
            
    $this->type $type;
        }

        
    /**
         * @param $item
         *
         * @return bool
         */
        
    protected function checkType($item): bool{
            
    $type $this->getType();
            return 
    $item instanceof $type;
        }

        
    /**
         * @return string
         */
        
    public function getType(): string{
            return 
    $this->type;
        }

        
    /**
         * @param string $type
         *
         * @return bool
         */
        
    public function isType(string $type): bool{
            return 
    $this->type === $type;
        }

        
    #region IteratorAggregate

        /**
         * @return \Traversable|$type
         */
        
    public function getIterator(): \Traversable{
            return new \
    ArrayIterator($this->items);
        }

        
    #endregion

        #region ArrayAccess

        /**
         * @param mixed $offset
         *
         * @return bool
         */
        
    public function offsetExists($offset){
            return isset(
    $this->items[$offset]);
        }

        
    /**
         * @param mixed $offset
         *
         * @return mixed|null
         */
        
    public function offsetGet($offset){
            return isset(
    $this->items[$offset]) ? $this->items[$offset] : null;
        }

        
    /**
         * @param mixed $offset
         * @param mixed $item
         */
        
    public function offsetSet($offset$item){
            if(!
    $this->checkType($item)){
                throw new \
    InvalidArgumentException('invalid type');
            }
            
    $offset !== null $this->items[$offset] = $item $this->items[] = $item;
        }

        
    /**
         * @param mixed $offset
         */
        
    public function offsetUnset($offset){
            unset(
    $this->items[$offset]);
        }

        
    #endregion
    }


    /**
     * Class Item
     */
    class Item{
        
    /**
         * @var int
         */
        
    public $id null;

        
    /**
         * @var string
         */
        
    public $data null;

        
    /**
         * @param int    $id
         * @param string $data
         */
        
    public function __construct(int $idstring $data){
            
    $this->id $id;
            
    $this->data $data;
        }
    }


    /**
     * Class ItemCollection
     */
    class ItemCollection extends GenericCollection{
        public function 
    __construct(){
            
    parent::__construct(Item::class);
        }

        
    /**
         * @return \Traversable|Item[]
         */
        
    public function getIterator(): \Traversable{
            return 
    parent::getIterator();
        }
    }


    /**
     * Class ExampleService
     */
    class ExampleService{
        
    /**
         * @var ItemCollection
         */
        
    private $items null;

        
    /**
         * @param ItemCollection $items
         */
        
    public function __construct(ItemCollection $items){
            
    $this->items $items;
        }

        
    /**
         * @return void
         */
        
    public function list(){
            foreach(
    $this->items as $item){
                echo 
    $item->data;
            }
        }
    }


    /**
     * Usage
     */
    $collection = new ItemCollection;
    $collection[] = new Item(1'foo');
    $collection[] = new Item(2'bar');
    $collection[] = new Item(3'foobar');

    $collection[] = 42// InvalidArgumentException: invalid type

    $service = new ExampleService($collection);
    $service->list(); 
    [SIZE="1"]Atwood's Law: any application that can be written in JavaScript, will eventually be written in JavaScript.[/SIZE]

    Kommentar

    Lädt...
    X