Skip to content

Instantly share code, notes, and snippets.

@janogarcia
Last active December 14, 2015 01:29
Show Gist options
  • Save janogarcia/5006393 to your computer and use it in GitHub Desktop.
Save janogarcia/5006393 to your computer and use it in GitHub Desktop.
PHP Bug: An object is automatically created if an unset object with a property is passed by reference. Inconsistent warnings depending on the variable type.
<?php
// Possible PHP Bug:
// An object is automatically created if an unset object with a property is passed by reference
// Inconsistent Warnings depending on the variable type
// Live test http://codepad.viper-7.com/ueZkz4
// Possibly related PHP bug https://bugs.php.net/bug.php?id=52237
// isset()
var_dump(isset($unset->property)); // FALSE
var_dump(isset($unset)); // FALSE
var_dump(is_object($unset)); // FALSE
var_dump(property_exists($unset, 'property')); // NULL
var_dump($unset->property); // NULL
$unset = FALSE;
var_dump(isset($unset->property)); // FALSE
unset($unset);
// isset_or()
function isset_or(&$var, $or = NULL)
{
return isset($var)? $var : $or;
}
var_dump(isset_or($unset->property)); // NULL, but... the $unset object is automagically created when passing by reference an unset variable with a property !?
var_dump(isset($unset)); // TRUE
var_dump(is_object($unset)); // TRUE
var_dump(property_exists($unset, 'property')); // TRUE
var_dump($unset->property); // NULL
$unset = NULL;
var_dump(isset_or($unset->property)); // NULL, no Warning
$unset = FALSE;
var_dump(isset_or($unset->property)); // NULL, no Warning
$unset = 0;
var_dump(isset_or($unset->property)); // NULL, Warning: Attempt to modify property of non-object
$unset = '';
var_dump(isset_or($unset->property)); // NULL, no Warning
$unset = array();
var_dump(isset_or($unset->property)); // NULL, Warning: Attempt to modify property of non-object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment