Created
December 3, 2016 20:53
-
-
Save klaussantana/0eb529bdd657f245ec71e92b7a52a83d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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