Skip to content

Instantly share code, notes, and snippets.

@a-sassermann
Created December 9, 2018 16:10
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 a-sassermann/840bd057f579169dc44bc4eee85b158a to your computer and use it in GitHub Desktop.
Save a-sassermann/840bd057f579169dc44bc4eee85b158a to your computer and use it in GitHub Desktop.
php >= 7.1 array_walk_recursive corrupts array value types
<?php
function test($mode){
$o = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
];
var_dump($o);
echo is_integer($o[\PDO::ATTR_ERRMODE ]).PHP_EOL;
echo is_integer($o[\PDO::ATTR_DEFAULT_FETCH_MODE ]).PHP_EOL;
if($mode != 'PDO'){
// simply by having a walk across the array seems to destroy an internal type indicator
array_walk_recursive($o, function ($value, $key) {
//important stuff here
});
// but is not visible to the user
var_dump($o);
}
if($mode == 'FIX-ARRAY-BY-CHECKING'){
// just check the value types and it gets fixed.
echo is_integer($o[\PDO::ATTR_ERRMODE ]).PHP_EOL;
echo is_integer($o[\PDO::ATTR_DEFAULT_FETCH_MODE ]).PHP_EOL;
// again no visible change
var_dump($o);
}
$p = new \PDO('sqlite:test.sq3', null, null, $o);
unset($p);
}
echo '**** Test 1 ***' . PHP_EOL;
echo 'Test PDO works as expected' . PHP_EOL;
echo '**** ****** ***' . PHP_EOL . PHP_EOL;
try{
test('PDO');
}catch(\Exception $exception){
echo $exception->getMessage();
}
echo PHP_EOL. PHP_EOL .'**** Test 2 ***' . PHP_EOL;
echo 'Test process PDO options with array_walk_recursive' . PHP_EOL;
echo '**** ****** ***' . PHP_EOL . PHP_EOL;
try{
test('ARRAY_WALK');
}catch(\Exception $exception){
echo $exception->getMessage();
}
echo PHP_EOL. PHP_EOL .'**** Test 3 ***' . PHP_EOL;
echo 'Test justify internal type by checking type of array values' . PHP_EOL;
echo '**** ****** ***' . PHP_EOL . PHP_EOL;
try{
test('FIX-ARRAY-BY-CHECKING');
}catch(\Exception $exception){
echo $exception->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment