Bug #74350 [Com]: defined() not working for $objectinstance::CLASS_CONSTANT

[email protected] ("Andy_Schmidt at HM-Software dot com")
Newsgroups php.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=74350&edit=1

 ID:                 74350
 Comment by:         Andy_Schmidt at HM-Software dot com
 Reported by:        Andy_Schmidt at HM-Software dot com
 Summary:            defined() not working for
                     $objectinstance::CLASS_CONSTANT
 Status:             Not a bug
 Type:               Bug
 Package:            Class/Object related
 Operating System:   Windows
 PHP Version:        7.1.3
 Block user comment: N
 Private report:     N

 New Comment:

Yes, I am aware how to circumvent this by obtaining the class first.

However, the point is:

 defined( 'Class_A::CONST_A' )       >>> WORKS
 defined( $class_name.'::CONST_A' )  >>> WORKS
 defined( "$class_name::CONST_A" )   >>> WORKS

But, I have found no working syntax for $object. That seems counter-intuitive, considering

 echo $object_A::CONST_A;	>>> WORKS

so "defined()" should represent that correct state.

PS - I do appreciate the dilemma presented by string substitution, object to string conversion etc. in this particular context.


Previous Comments:
------------------------------------------------------------------------
[2017-03-31 18:03:24] [email protected]

The reason you get "not" defined! is because the instance is within single quotes.

Which means it will mean a literal `$object_A` because normal string rules apply.

You can always do:

if ( defined( get_class($object_A) . '::CONST_A' ) )

to get what you want.

------------------------------------------------------------------------
[2017-03-31 17:50:06] Andy_Schmidt at HM-Software dot com

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 this bug report at https://bugs.php.net/bug.php?id=74350&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.