php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29986 Class constants won't work with predefined constants when using ReflectionClass
Submitted: 2004-09-05 15:40 UTC Modified: 2006-02-25 00:31 UTC
Votes:2
Avg. Score:4.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: mitchel at sahertian dot com Assigned: johannes (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.0.1 OS: Linux 2.6.8.1 x86
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mitchel at sahertian dot com
New email:
PHP Version: OS:

 

 [2004-09-05 15:40 UTC] mitchel at sahertian dot com
Description:
------------
I have a variable classname i have to get a constant/static property from.
${$classname}::stuff
doesn't work, so i have to use the reflection api.
This works for strings and numbers, but when i try to use getConstant() upon a constant that is defined as another constant, it returns UNKNOWN:0.

This happens for user defined constants as well as things like `true'.

Reproduce code:
---------------
<?php
define("FOOBAR",0x0008):

class Myclass {
  const foo = true;
  const bar = FOOBAR;
  const works = "yes it does";
}

$o=new ReflectionClass("MyClass");
var_dump($o->getConstant("foo"));
var_dump($o->getConstant("bar"));
var_dump($o->getConstant("works"));
?>

Expected result:
----------------
bool(true)
int(8)
string(11) "yes it does"

Actual result:
--------------
UNKNOWN:0
UNKNOWN:0
string(11) "yes it does"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-09-08 16:08 UTC] ante dot dfg at moj dot net
Interestingly enough this example(btw there's an error at end of DEFINE - define("FOOBAR",0x0008): <- this should be ;) when copy paste and run dies with Fatal error: Trying to clone an uncloneable object of class ReflectionClass in c:\serverroot\test.php on line 11

but If I put a reference in $o=new ReflectionClass("MyClass");
making it $o=&new ReflectionClass("MyClass");

then I actualy get the message ("actual result") that Michael stated....

I'm using php 5.0.1 (release version) on XP Pro under Apache 1.3.31....

Hope this helps somehow...
 [2004-10-12 09:27 UTC] php at bobsilva dot com
This is fixed in 5.1 CVS
 [2004-10-12 09:33 UTC] derick@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2004-10-20 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-02-24 15:52 UTC] RQuadling at GMail dot com
Windows XP SP2 running the latest PHP.

PHP 5.1.3-dev (cli) (built: Feb 20 2006 00:29:25)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies
    with Zend Extension Manager v1.0.10, Copyright (c) 2003-2006, by Zend Technologies
    with Zend Optimizer v3.0.0-Beta2, Copyright (c) 1998-2006, by Zend Technologies




Reproduce code:
---------------
<?php
class just_constants
	{
	// These don't work.
	const BOOLEAN_CONSTANT = True;
	const NULL_CONSTANT = Null;

	// These do work.
	const STRING_CONSTANT = 'This is a string';
	const INTEGER_CONSTANT = 1000;
	const FLOAT_CONSTANT = 3.1415926535897932384626433832795;
	}

Reflection::export(new ReflectionClass('just_constants'));
?>

Expected result:
----------------
Class [ <user> class just_constants ] {
  @@ D:\Data\PHP\Includes\ERT\rcerr.php 2-10

  - Constants [5] {
    Constant [ boolean BOOLEAN_CONSTANT ] { }
    Constant [ null NULL_CONSTANT ] { }
    Constant [ string STRING_CONSTANT ] { }
    Constant [ integer INTEGER_CONSTANT ] { }
    Constant [ double FLOAT_CONSTANT ] { }
  }

  - Static properties [0] {
  }

  - Static methods [0] {
  }

  - Properties [0] {
  }

  - Methods [0] {
  }
}

Actual result:
--------------
Class [ <user> class just_constants ] {
  @@ D:\Data\PHP\Includes\ERT\rcerr.php 2-10

  - Constants [5] {
    Constant [ unknown BOOLEAN_CONSTANT ] { }
    Constant [ unknown NULL_CONSTANT ] { }
    Constant [ string STRING_CONSTANT ] { }
    Constant [ integer INTEGER_CONSTANT ] { }
    Constant [ double FLOAT_CONSTANT ] { }
  }

  - Static properties [0] {
  }

  - Static methods [0] {
  }

  - Properties [0] {
  }

  - Methods [0] {
  }
}

In zend_API.c (/* $Id: zend_API.c,v 1.347 2006/02/22 12:02:44 dmitry Exp $ */) Lines 217 to 250 - zend_zval_type_name() has the capability to return the 'boolean' and 'null' values. Though 'string' is shown as 'native string' in the source code and that change was made V1.304 17/08/2005 but has not been compiled maybe? Is the zend engine slow to update as compared to the main PHP code? Are there other changes not being made here? I did a binary search on all of the dlls and exes and no mention of 'native string'. At around byte 0x2728B0 in php5ts.dll the text for the return values from the zend_zval_type_name function are stored.
 [2006-02-25 00:31 UTC] johannes@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 26 22:00:03 2025 UTC