[PHP-BUG] Bug #74350 [NEW]: defined() not working for $objectinstance::CLASS_CONSTANT
[email protected] ("Andy_Schmidt at HM-Software dot com")
| Newsgroups | php.bugs |
|---|---|
| Message-ID | <[email protected]> |
From: Andy_Schmidt at HM-Software dot com
Operating system: Windows
PHP version: 7.1.3
Package: Class/Object related
Bug Type: Bug
Bug description:defined() not working for $objectinstance::CLASS_CONSTANT
Description:
------------
defined() will function correctly for class constants, if either the
class name or a classname variable is used.
However, if one tries to check the existence of a constant of the class
that was used to instantiate the object, then it will return "not
defined".
Test script:
---------------
class Class_A
{
const CONST_A = 'value A';
// CONST_B not defined
}
$class_name = Class_A::class;
$object_A = new $class_name();
// Class references work correctly!
if ( defined( 'Class_A::CONST_A' ) )
echo 'Class_A::CONST_A defined';
if ( defined( $class_name.'::CONST_A' ) )
echo '$class_name::CONST_A defined';
if ( !defined( 'Class_A::CONST_B' ) )
echo 'No Class_A::CONST_B';
if ( !defined( $class_name.'::CONST_B' ) )
echo 'No $class_name::CONST_B';
echo $object_A::CONST_A; // As expected, result: "value A" ! ****
// ***** Yet, using that object reference will report "not" defined!
*****
if ( defined( '$object_A::CONST_A' ) )
echo '$object_A::CONST_A defined !!';
else
echo 'no $object_A::CONST_A';
// PS: As expected, "Fatal Error: Object of class Class_A could not be
converted to string"
if ( defined( "$object_A::CONST_A" ) )
echo 'Should cause error, and does';
if ( defined( $object_A.'::CONST_A' ) )
echo 'Should cause error, and does';
Expected result:
----------------
Class_A::CONST_A defined
$class_name::CONST_A defined
No Class_A::CONST_B
No $class_name::CONST_B
value A
$object_A::CONST_A defined!!
Actual result:
--------------
Class_A::CONST_A defined
$class_name::CONST_A defined
No Class_A::CONST_B
No $class_name::CONST_B
value A
no $object_A::CONST_A
--
Edit bug report at https://bugs.php.net/bug.php?id=74350&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=74350&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=74350&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=74350&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=74350&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=74350&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=74350&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=74350&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=74350&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=74350&r=support
Expected behavior: https://bugs.php.net/fix.php?id=74350&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=74350&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=74350&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=74350&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=74350&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=74350&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=74350&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=74350&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=74350&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=74350&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=74350&r=mysqlcfg