Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save C-Duv/fb40e0ad516f8dbccd2e to your computer and use it in GitHub Desktop.
Save C-Duv/fb40e0ad516f8dbccd2e to your computer and use it in GitHub Desktop.
PHP snippet to highlight an array cursor being modified from outside an object or a class
<?php
/**
* Snippet to highlight an array cursor being modified from outside an object or
* a class
**/
class MyClass
{
protected $arr = [];
protected static $arrStatic = [];
function __construct($array = []) {
$this->arr = $array;
self::$arrStatic = $array;
}
function getArr() { return $this->arr; }
function getArrStatic() { return self::$arrStatic; }
function getCurrent() { return current($this->arr); }
function getCurrentStatic() { return current(self::$arrStatic); }
}
echo 'phpversion() = ' . phpversion() . PHP_EOL . PHP_EOL;
$o = new MyClass([1, 2, 3]);
echo '__Instance: initial state__' . PHP_EOL;
echo 'MyClass::getCurrent() = '; var_dump($o->getCurrent());
echo 'current(MyClass::getArr()) = '; var_dump(current($o->getArr()));
$a = $o->getArr();
echo 'current($a) = '; var_dump(current($a));
echo '$a = ';
print_r($a);
echo 'MyClass::getCurrent() = '; var_dump($o->getCurrent());
echo 'current(MyClass::getArr()) = '; var_dump(current($o->getArr()));
echo 'current($a) = '; var_dump(current($a));
unset($a);
echo PHP_EOL;
echo '__foreach with accessor__' . PHP_EOL;
foreach ($o->getArr() as $item) {
echo $item . ' ';
}
echo PHP_EOL;
echo 'MyClass::getCurrent() = '; var_dump($o->getCurrent());
echo 'current(MyClass::getArr()) = '; var_dump(current($o->getArr()));
echo PHP_EOL;
echo '__foreach with local var__' . PHP_EOL;
$a = $o->getArr();
foreach ($a as $item) {
echo $item . ' ';
}
echo PHP_EOL;
echo 'MyClass::getCurrent() = '; var_dump($o->getCurrent());
echo 'current(MyClass::getArr()) = '; var_dump(current($o->getArr()));
echo 'current($a) = '; var_dump(current($a));
echo '------------------------------------' . PHP_EOL;
echo '__Static: initial state__' . PHP_EOL;
echo 'MyClass::getCurrentStatic() = '; var_dump($o->getCurrentStatic());
echo 'current(MyClass::getArrStatic()) = '; var_dump(current($o->getArrStatic()));
$as = $o->getArrStatic();
echo 'current($as) = '; var_dump(current($as));
echo '$as = ';
print_r($as);
echo 'MyClass::getCurrentStatic() = '; var_dump($o->getCurrentStatic());
echo 'current(MyClass::getArrStatic()) = '; var_dump(current($o->getArrStatic()));
echo 'current($as) = '; var_dump(current($as));
unset($as);
echo PHP_EOL;
echo '__foreach with accessor__' . PHP_EOL;
foreach ($o->getArrStatic() as $item) {
echo $item . ' ';
}
echo PHP_EOL;
echo 'MyClass::getCurrentStatic() = '; var_dump($o->getCurrentStatic());
echo 'current(MyClass::getArrStatic()) = '; var_dump(current($o->getArrStatic()));
echo PHP_EOL;
echo '__foreach with local var__' . PHP_EOL;
$as = $o->getArrStatic();
foreach ($as as $item) {
echo $item . ' ';
}
echo PHP_EOL;
echo 'MyClass::getCurrentStatic() = '; var_dump($o->getCurrentStatic());
echo 'current(MyClass::getArrStatic()) = '; var_dump(current($o->getArrStatic()));
echo 'current($as) = '; var_dump(current($as));
phpversion() = 5.5.9-1ubuntu4.11
__Instance: initial state__
MyClass::getCurrent() = int(1)
current(MyClass::getArr()) = int(1)
current($a) = int(1)
$a = Array
(
[0] => 1
[1] => 2
[2] => 3
)
MyClass::getCurrent() = int(1)
current(MyClass::getArr()) = int(1)
current($a) = int(1)
__foreach with accessor__
1 2 3
MyClass::getCurrent() = bool(false)
current(MyClass::getArr()) = int(1)
__foreach with local var__
1 2 3
MyClass::getCurrent() = int(1)
current(MyClass::getArr()) = int(1)
current($a) = bool(false)
------------------------------------
__Static: initial state__
MyClass::getCurrentStatic() = int(1)
current(MyClass::getArrStatic()) = int(1)
current($as) = int(1)
$as = Array
(
[0] => 1
[1] => 2
[2] => 3
)
MyClass::getCurrentStatic() = int(1)
current(MyClass::getArrStatic()) = int(1)
current($as) = int(1)
__foreach with accessor__
1 2 3
MyClass::getCurrentStatic() = bool(false)
current(MyClass::getArrStatic()) = int(1)
__foreach with local var__
1 2 3
MyClass::getCurrentStatic() = int(1)
current(MyClass::getArrStatic()) = int(1)
current($as) = bool(false)
@C-Duv
Copy link
Author

C-Duv commented Jul 21, 2015

Lines 19 and 44 shows that foreach() moved the returned-array's cursor (it is now beyond the end of the elements).

@C-Duv
Copy link
Author

C-Duv commented Jul 21, 2015

It turns out to be an "issue" the PHP RFC: Fix "foreach" behavior is trying to fix. When using foreach, the internal pointer is altered.

(Thanks @pmartin)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment