|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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"
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 08 19:00:02 2025 UTC |
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...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.