php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74350 defined() not working for $objectinstance::CLASS_CONSTANT
Submitted: 2017-03-31 17:50 UTC Modified: 2017-03-31 18:03 UTC
From: Andy_Schmidt at HM-Software dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 7.1.3 OS: Windows
Private report: No CVE-ID: None
 [2017-03-31 17:50 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-31 18:03 UTC] peehaa@php.net
-Status: Open +Status: Not a bug
 [2017-03-31 18:03 UTC] peehaa@php.net
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 18:12 UTC] Andy_Schmidt at HM-Software dot com
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 21:01:27 2024 UTC