php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48209 Namespaces and get_class()
Submitted: 2009-05-09 14:17 UTC Modified: 2009-05-10 15:37 UTC
From: macmade at eosgarden dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.0RC2 OS: MacOS X 10.5.6 (Client)
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: macmade at eosgarden dot com
New email:
PHP Version: OS:

 

 [2009-05-09 14:17 UTC] macmade at eosgarden dot com
Description:
------------
When using the get_class() function on an object whose class is defined in a different namespace as the actual, the return value does not contain a leading backslah (which is normal). But then why are we able to dynamically create an instance, based on the result of get_class(), without adding a leading backslash?

I don't know if this is a bug, a feature, a wanted or un-wanted behavior, but I think this is weird.

Reproduce code:
---------------
<?php

namespace Foo;

class Test
{}

namespace Bar;

# Calling 'new Foo\Test()' will obvisously fail, as it won't be found in the current NS
$TEST      = new \Foo\Test(); 

$CLASSNAME = get_class( $TEST );

# Will display 'Foo\Test' - No leading backslash
print $CLASSNAME . '<br />';

# This will work... Why???
print_r( new $CLASSNAME() );

Expected result:
----------------
Foo\Test
Fatal error: Class 'Bar\Foo\Test' not found ... on line 19

Actual result:
--------------
Foo\Test
Foo\Test Object ( ) 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-10 09:43 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

using string variables as classname will always look from the global NS
 [2009-05-10 15:37 UTC] macmade at eosgarden dot com
Sorry for the annoyance... : )
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 12:01:33 2025 UTC