php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #22749 Classes outside of namespaces cannot be called.
Submitted: 2003-03-17 11:33 UTC Modified: 2003-07-20 08:53 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: eric at evilwalrus dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: *
Private report: No CVE-ID: None
 [2003-03-17 11:33 UTC] eric at evilwalrus dot com
If I have the following script, the class that is defined before the namespace cannot be called from within a class in the namespace.

<?php
class TestClass2
{
    function __construct()
    {
        print "TestClass2\n";
    }
}

namespace Test
{
    function newClass () {
        $new = new TestClass();
        $new->newClass();
    }
    
    class TestClass
    {
        function __construct()
        {
            print "TestClass\n";
        }
        
        function newClass ()
        {
            $new = new TestClass2();
        }
    }
}

Test::newClass();
//$new = new TestClass2();
?>

This example will show the error "Fatal error: Class 'testclass2' not found in c:\usr\test.php on line 26" I'm assuming that this is done by design... but I would think that PHP would look for the class outside of the namespace since it is defined before the namespace. If you use the commented out line $new = new TestClass2; instead of the Test::newClass(); then it works properly, and the TestClass2 construct is called.

As I said, I think this is done by design and isn't a bug... but I thought I would submit it because I'm not for sure. Any explination though would be very helpful to me. Thanks.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-27 17:58 UTC] mfischer@php.net
Also pretty annoying if you want to declare a class extending the main exception class in your namespace. It simply doesn't work because its not "seen":

$ php -r 'class myexception extends exception { } '
[no error, works]

$ php -r 'namespace myscope { class myexception extends exception { } }'
Command line code(1) : Fatal error - Class 'exception' not found

I couldn't find a way to reference the global namespace.
 [2003-03-29 04:15 UTC] mfischer@php.net
Thanks to Marcus who brought light into this:

You access classes from the global namespace by ommiting the namespace-name part:

new ::TestClass2();

is the proper syntax. Same for my exception extending problem:

php -r 'namespace myscope { class myexception extends ::exception { } }'

Marking as a documentation problem (it's not in ZEND_CHANGES even).
 [2003-03-29 14:59 UTC] eric at evilwalrus dot com
This would be why I thought it was by design. That works for me. I just changed the $new = new TestClass2(); to $new = new ::TestClass2(); and it worked perfectly. Thanks.
 [2003-07-20 08:53 UTC] sniper@php.net
There are no namespaces anymore. (PHP 5)

 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Sep 16 23:03:39 2021 UTC