hiphop-php /hphp/test/slow/array_access/436.php

Language PHP Lines 102
MD5 Hash 3dade00dcc7fb11d57132d15d284adb7 Estimated Cost $2,383 (why?)
Repository git://github.com/facebook/hiphop-php.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
<?php

function offsetGet($index) {
  echo ("GET0: $index\n");
}
class ArrayAccessImpl implements ArrayAccess {
  public $data = array();
  public function offsetUnset($index) {
 echo "UNSET: $index\n";
 }
  public function offsetSet($index, $value) {
    echo ("SET: $index\n");
    if(isset($data[$index])) {
        unset($data[$index]);
    }
    $u = &$this->data[$index];
    if(is_array($value)) {
        $u = new ArrayAccessImpl();
        foreach($value as $idx=>$e)            $u[$idx]=$e;
    }
 else        $u=$value;
  }
  public function offsetGet($index) {
    echo ("GET: $index\n");
    if(!isset($this->data[$index]))        $this->data[$index]=new ArrayAccessImpl();
    return $this->data[$index];
  }
  public function offsetExists($index) {
    echo ("EXISTS: $index\n");
    if(isset($this->data[$index])) {
        if($this->data[$index] instanceof ArrayAccessImpl) {
            if(count($this->data[$index]->data)>0)                return true;
            else                return false;
        }
 else            return true;
    }
 else        return false;
  }
}
class ArrayAccessImpl2 extends ArrayAccessImpl {
  public function offsetUnset($index) {
 echo "UNSET2: $index\n";
 }
  public function offsetSet($index, $value) {
    echo ("SET2: $index\n");
    if(isset($data[$index])) {
        unset($data[$index]);
    }
    $u = &$this->data[$index];
    if(is_array($value)) {
        $u = new ArrayAccessImpl();
        foreach($value as $idx=>$e)            $u[$idx]=$e;
    }
 else        $u=$value;
  }
  public function offsetGet($index) {
    echo ("GET2: $index\n");
    if(!isset($this->data[$index]))        $this->data[$index]=new ArrayAccessImpl();
    return $this->data[$index];
  }
  public function offsetExists($index) {
    echo ("EXISTS2: $index\n");
    if(isset($this->data[$index])) {
        if($this->data[$index] instanceof ArrayAccessImpl) {
            if(count($this->data[$index]->data)>0)                return true;
            else                return false;
        }
 else            return true;
    }
 else        return false;
  }
}
offsetGet('foo');
$data = new ArrayAccessImpl();
$data['string']="Just a simple string";
$data['number']=33;
$data['array']['another_string']="Alpha";
$data['array']['some_object']=new stdClass();
$data['array']['another_array']['x']['y']="Beta";
$data['blank_array']=array();
print_r(isset($data['array']));
print_r($data['array']['non_existent']);
print_r(isset($data['array']['non_existent']));
print_r($data['blank_array']);
print_r(isset($data['blank_array']));
unset($data['blank_array']);
print_r($data);
$data2 = new ArrayAccessImpl2();
$data2['string']="Just a simple string";
$data2['number']=33;
$data2['array']['another_string']="Alpha";
$data2['array']['some_object']=new stdClass();
$data2['array']['another_array']['x']['y']="Beta";
$data2['blank_array']=array();
print_r(isset($data2['array']));
print_r($data2['array']['non_existent']);
print_r(isset($data2['array']['non_existent']));
print_r($data2['blank_array']);
print_r(isset($data2['blank_array']));
unset($data2['blank_array']);
print_r($data2);
Back to Top