# define()d constants in constant arrays break on inheritance

- Thread: https://externals.io/message/2178
- Original archive: https://news-web.php.net/php.internals/2178
- Started by Wez Furlong on 2003-06-05T08:56:52+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #2178 — Wez Furlong — 2003-06-05T08:56:52+00:00

URL: https://externals.io/message/2178#2178

This bug has been around a long time; I actually thought that `define()`s
wouldn't work at all in scalar initializers until today :)

<?php
error_reporting(E_ALL);
define('A', 200);
class Foo { var $bar = array(A => 'a'); }
class Bar extends Foo {}

var_dump(get_class_vars('Foo')); // bar => array(200 => 'a')
var_dump(get_class_vars('Bar')); // bar => array('A' => 'a')
?>

If you comment out the first get_class_vars line, then the output for
Bar is correct (200 => 'a').

I tracked down the problem to zval_update_constant(); when it walks the
array it turns of the IS_CONSTANT_INDEX bit on the original zval.  So,
when it is called subsequently it doesn't know that it should lookup the
constant name.

The patch at http://www.php.net/~wez/define_inherit.diff fixes this
problem by making a copy of the element and clearing the bit in the
copy, and removing the reference to the original element from the array.

--Wez.
