Bug #73647 [Nab]: stdClass is not inherited if the class definition doesn't explicit it.
| From: | requinix@php.net | Date: | Sun, 04 Dec 2016 08:47:34 +0000 |
| Subject: | Bug #73647 [Nab]: stdClass is not inherited if the class definition doesn't explicit it. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-205760@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=73647&edit=1
ID: 73647
Updated by: requinix@php.net
Reported by: klaussantana at gmail dot com
Summary: stdClass is not inherited if the class definition
doesn't explicit it.
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: any
PHP Version: 7.0.13
Block user comment: N
Private report: N
New Comment:
> All classes defined without the
extends expression should be
> implied as an
> extension of stdClass.
That only works if stdClass is an implied base class. It is not. UserLand and stdClass are
completely and entirely unrelated classes; they have as much in common as UserLand and SplFileInfo
do.
If you want this as a feature request, that's fine, but a change like this would probably
require an RFC. https://wiki.php.net/rfc/howto
And I still don't understand why you can't just use is_object().
Previous Comments:
------------------------------------------------------------------------
[2016-12-04 08:02:23] klaussantana at gmail dot com
requinix@php.net
You missed my comment right after publishing.. All classes defined without the extends
expression should be implied as an extension of stdClass.
The following code should produce no error, but it does:
<?PHP class UserLand {}
$f = function ( stdClass $Object = null )
{
# I want any object here that is not built-in classes like those: Closure, Exception, PDO, etc...
}
$f(new UserLand); // Doesn't work as of UserLand do not explicit extends
stdClass.
// So it should be reasonable to UserLand pass an instanceof test against
stdClass.
?>
Should I change this to "Feature/Change Request" instead?
------------------------------------------------------------------------
[2016-12-03 21:37:49] requinix@php.net
PHP does not have an implicit base class like C#'s System.Object or Java's
java.lang.Object. stdClass is just a built-in, empty class.
If you want to know whether something is an object then use is_object().
------------------------------------------------------------------------
[2016-12-03 21:14:51] klaussantana at gmail dot com
This causes a serious problem when you try something like this:
<?PHP class Insecure {}
(
function ( stdClass $Object = null )
{
# Some stuff only for stdClass inheritants...
return $Object;
}
) ( new Insecure );
------------------------------------------------------------------------
[2016-12-03 21:04:54] klaussantana at gmail dot com
Description:
------------
For some reason, if you not explicit extends a class from stdClass it will not pass the
instanceof test.
Test script:
---------------
<?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 =
[
'Instable Object' => new InstableDefinition,
'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;
Expected result:
----------------
The InstableDefinition class objects does instance from stdClass.
The StableDefinition class objects does instance from stdClass.
The AnotherInstableDefinition class objects does instance from stdClass.
The AnotherStableDefinition class objects does instance from stdClass.
All these tests ran under PHP version 7.0.13.
Actual result:
--------------
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 7.0.13.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=73647&edit=1