Skip to content

Instantly share code, notes, and snippets.

@klaussantana
Created December 3, 2016 20:53
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 klaussantana/0eb529bdd657f245ec71e92b7a52a83d to your computer and use it in GitHub Desktop.
Save klaussantana/0eb529bdd657f245ec71e92b7a52a83d to your computer and use it in GitHub Desktop.
<?PHP
/****
* stdClass Inheritance Failure
* ================================================================================
*
* This example shows how stdClass is not inherited in classes definitions that
* do not explicit it.
*
*/
class InstableDefinition {}
class StableDefinition extends stdClass {}
class AnotherInstableDefinition extends InstableDefinition {}
class AnotherStableDefinition extends StableDefinition {}
$Objects =
[
'First Instable Object' => new InstableDefinition,
'First Stable Object' => new StableDefinition,
'Another Instable Object' => new AnotherInstableDefinition,
'Another Stable Object' => new AnotherStableDefinition,
];
foreach ( $Objects as $Object )
{
$ObjectClass = get_class($Object);
$ObjectStandard = $Object instanceof stdClass;
$ObjectEvaluates = $ObjectStandard ? 'does' : "doesn't";
echo "The `{$ObjectClass}` class objects {$ObjectEvaluates} instance from `stdClass`." .PHP_EOL;
}
echo 'All these tests ran under PHP version ' .PHP_VERSION .'.' .PHP_EOL;
/****
* This script will produce the following text:
*
* The `InstableDefinition` class objects doesn't instance from `stdClass`.
* The `StableDefinition` class objects does instance from `stdClass`.
* The `AnotherInstableDefinition` class objects doesn't instance from `stdClass`.
* The `AnotherStableDefinition` class objects does instance from `stdClass`.
* All these tests ran under PHP version X.X.XX.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment