# unable to create class from constant

- Thread: https://externals.io/message/19076
- Original archive: https://news-web.php.net/php.internals/19076
- Started by Jason Sweat on 2005-09-19T15:18:10+00:00
- Messages: 5
- 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

---

## #19076 — Jason Sweat — 2005-09-19T15:18:10+00:00

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

I ran across this today and it surprised me:

$ php -r 'define("X", "stdClass"); $x = new X; var_dump($x);';
Fatal error: Cannot instantiate non-existent class:  x in Command line
code on line 1

I tested on php 4.4.0, 4.3.8 and 5.0.3, with the result that all
versions exhibit the same behavior.

Clearly there is an easy work around:

$  php -r 'define("X", "stdClass"); $y = X; $x = new $y; var_dump($x);'
object(stdClass)(0) {
}

But I was wondering if anyone could enlighten me as to why php would
be upset interpreting the constant as the class name.  Thanks.
 
Regards,
Jason
http://blog.casey-sweat.us/

---

## #19077 — Sean Coates — 2005-09-19T15:55:11+00:00

URL: https://externals.io/message/19076#19077 · In reply to: #19076

> But I was wondering if anyone could enlighten me as to why php would
> be upset interpreting the constant as the class name.  Thanks.

class Foo {
  function get_name() { return 'Foo'; }
}

class Bar {
  function get_name() { return 'Bar'; }
}

define('Foo', 'Bar');

$C = new Foo();
echo $C->get_name();
// output: Foo
// you might have been expecting: Bar

This "issue" also affects static method calls:
Foo::get_name();
// returns: Foo
// even though Foo (the constant) resolves to: Bar

HTH.

S

---

## #19078 — Xuefer — 2005-09-19T16:03:03+00:00

URL: https://externals.io/message/19076#19078 · In reply to: #19076

can u tell me why
php -r 'function myfunc(){} define("X", "myfunc"); X(); '
Fatal error: Call to undefined function:  x() in Command line code on line 1
but not calling myfunc?

wrong list. :)

---

## #19080 — Jason Sweat — 2005-09-19T16:58:31+00:00

URL: https://externals.io/message/19076#19080 · In reply to: #19078

> wrong list. :)

My question was not "how do I work around this?".  I included that in
my original post.  My question was "why is it like this?" which I
thought was more germane to the internals list.

An unquoted string would have to first be thought of as the class
name, but if that does not exist, php is not then checking to see if
it is actually a defined constant.

My questions was basically is this a performance issue or just an
undesired behavior for some other reason?
 
Regards,
Jason
http://blog.casey-sweat.us/

---

## #19081 — Marcus Boerger — 2005-09-19T17:01:55+00:00

URL: https://externals.io/message/19076#19081 · In reply to: #19080

Hello Jason,

  new doesn't expect a string which a constant in fact is. It expects a
class name. Thus the class name cannot be constified. You'd need to use
reflection for that:
$r = new ReflectionClass($name); $o = $r->newInstance();

best regards
marcus

Monday, September 19, 2005, 6:58:31 PM, you wrote:

> 
> > […]

> My question was not "how do I work around this?".  I included that in
> my original post.  My question was "why is it like this?" which I
> thought was more germane to the internals list.

> An unquoted string would have to first be thought of as the class
> name, but if that does not exist, php is not then checking to see if
> it is actually a defined constant.

> My questions was basically is this a performance issue or just an
> undesired behavior for some other reason?
>  
> Regards,
> Jason
> http://blog.casey-sweat.us/

> --
> 
> 




Best regards,
 Marcus
