The Collection interface

(PECL ds >= 1.0.0)

Introduzione

Collection is the base interface which covers functionality common to all the data structures in this library. It guarantees that all structures are traversable, countable, and can be converted to json using json_encode().

Sommario dell'interfaccia

interface Ds\Collection extends Countable, IteratorAggregate, JsonSerializable {
/* Metodi */
public clear(): void
public isEmpty(): bool
public toArray(): array
/* Metodi ereditati */
}

Log delle modifiche

Versione Descrizione
PECL ds 1.4.0 Collection implements IteratorAggregate now instead of just Traversable. (This change came to the polyfill in 1.4.1.)

Indice dei contenuti

add a note add a note

User Contributed Notes 1 note

up
-11
depakin at yisangwu dot com
4 years ago
// Collection
   $collection_a = new \Ds\Vector([1, 2, 3]);
   $collection_b = new \Ds\Vector();

   var_dump($collection_a, $collection_b);
    /*
        object(Ds\Vector)[1]
        public 0 => int 1
        public 1 => int 2
        public 2 => int 3

        object(Ds\Vector)[2]
    */

   //json_encode
   var_dump( json_encode($collection_a));
    /*
    string '[1,2,3]
    */
  
    //count
   var_dump(count($collection_a));
    /*
    int 3
    */
  
    // serialize
    var_dump(serialize($collection_a));
    /*
    string 'C:9:"Ds\Vector":12:{i:1;i:2;i:3;}'
    */

    // foreach
    foreach ($collection_a as $key => $value) {
       echo $key ,'--', $value, PHP_EOL;
    }
    /*
       0--1
       1--2
       2--3
     */

    // clone
    $clone = clone($collection_a);
    var_dump($clone);
    /*
      object(Ds\Vector)[1]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3
    */

    // push
    $clone->push('aa');
    var_dump($clone);
    /*
    object(Ds\Vector)[3]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3
      public 3 => string 'aa' (length=2)
     */

   // isEmpty
   var_dump($collection_a->isEmpty(), $collection_b->isEmpty());
    /*
    boolean false
    boolean true
     */

   // toArray
   var_dump($collection_a->toArray(), $collection_b->toArray());
    /*
     array (size=3)
      0 => int 1
      1 => int 2
      2 => int 3

    array (size=0)
      empty
    */

   // copy ( void )
   //浅拷贝, shallow copy
   $collection_c = $collection_a->copy();

   var_dump($collection_c);
    /*
    object(Ds\Vector)[3]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3
    */

   $collection_c->push(4);
   var_dump($collection_a, $collection_c);
    /*
    object(Ds\Vector)[1]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3

    object(Ds\Vector)[3]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3
      public 3 => int 4
    */
  
    // clear
    $collection_a->clear();
    $collection_b->clear();
    $collection_c->clear();

    var_dump($collection_a, $collection_b, $collection_c);
    /*
    object(Ds\Vector)[1]
    object(Ds\Vector)[2]
    object(Ds\Vector)[3]
    */
To Top