Skip to content

Instantly share code, notes, and snippets.

@kijtra
Last active August 29, 2015 14:15
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 kijtra/78c54a12cfbf3c4432f3 to your computer and use it in GitHub Desktop.
Save kijtra/78c54a12cfbf3c4432f3 to your computer and use it in GitHub Desktop.
#PHP 変数のSet/Get用関数
<?php
function vars($keys = null, $value = null)
{
static $values = array(), $buffer = array();
if (empty($keys)) {
return $values;
}
elseif (is_string($keys)) {
//$keys = strtolower($keys);
if (null !== $value) {
if (false !== strpos($keys, '.')) {
$ex = explode('.', $keys);
$current = $ex[0];
$data = array();
$ref =& $data;
foreach ($ex as $key) {
$ref =& $ref[$key];
}
$ref = $value;
unset($ref);
$values[$current] = array_replace_recursive($values[$current], $data[$current]);
return $buffer[$keys] = $value;
} else {
$values[$keys] = $value;
return $value;
}
} elseif (!empty($values[$keys])) {
return $values[$keys];
} elseif (!empty($buffer[$keys])) {
return $buffer[$keys];
} else {
$value = $values;
foreach (explode('.', $keys) as $key) {
if (!array_key_exists($key, $value)) {
return null;
}
$value = $value[$key];
}
return $buffer[$keys] = $value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment