Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use a Namespaced Class inside another namespace in PHP

Tags:

namespaces

php

I am still having trouble with PHP5 Namespaces.

I have a namespace called Project and I am trying to access a class called registry inside of this Project namespace that has a namespace of Library so at the top of the file that is a Project namespace I use this line use Library\Registry;

Registry class is inside the Library Namespace

This should work but it doesn't, instead the ONLY way to access my registry class inside this Project namespace is to use this

$this->registry = new \Library\Registry;

I want to be able to use this instead...

$this->registry = new Registry;

That was the whole reason of using

use Library\Registry;

at the top of the Project namespace file


Below I have 3 small example scripts in a folder structure like this.
Library/registry.class.php a class in my Library folder
Controller/controller.class.php and class in my Controller directory
Controller/testing.php a test file to run the script.

E:\Library\Registry.class.php file

<?php
namespace Library
{
    class Registry
    {
        function __construct()
        {
            echo 'Registry.class.php Constructor was ran';
        }
    }
}
?>

E:\Controller\Controller.class.php file

<?php
use Library\Registry;

namespace Project
{
    class Controller
    {
        public $registry;

        function __construct()
        {
            include('E:\Library\Registry.class.php');

            // This is where my trouble is
            // to make it work currently I have to use
            //  $this->registry = new /Library/Registry;
            // But I want to be able to use it like below and that is why
            // I have the `use Library\Registry;` at the top
            $this->registry = new Registry;
        }

        function show()
        {
            $this->registry;
            echo '<br>Registry was ran inside testcontroller.php<br>';
        }
    }
}
?>

E:\Controller\testing.php file

<?php
use Project\Controller;

include('testcontroller.php');

$controller = new Controller();
$controller->show();

?>

I get this error...

Fatal error: Class 'Project\Registry' not found in PATH to file

unless I use this below on the controller.class.php file

$this->registry = new \MyLibrary\Registry;

Because in that file at the top I have use Library\Registry; I should be able to access it like this...

$this->registry = new Registry;

Please help me get it where I can use it like that instead

like image 502
JasonDavis Avatar asked Dec 23 '11 14:12

JasonDavis


People also ask

How can use namespace in another file in PHP?

A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the declare keyword. Add this to your included file as well. namespace my_ns; After that, your code works just fine.

What is alias in PHP?

Using namespaces: Aliasing/Importing ¶ (PHP 5 >= 5.3.0, PHP 7, PHP 8) The ability to refer to an external fully qualified name with an alias, or importing, is an important feature of namespaces. This is similar to the ability of unix-based filesystems to create symbolic links to a file or to a directory.

How do namespaces work in PHP?

Like C++, PHP Namespaces are the way of encapsulating items so that same names can be reused without name conflicts. It can be seen as an abstract concept in many places. It allows redeclaring the same functions/classes/interfaces/constant functions in the separate namespace without getting the fatal error.

What is the use of use keyword in PHP?

The use keyword has two purposes: it tells a class to inherit a trait and it gives an alias to a namespace.


2 Answers

use Library\Registry;

namespace Project
{

I believe that's the wrong way round: you're useing Library\Registry in the global namespace, and then opening the Project namespace.

Put the use statement inside the namespace you want to import it into.

namespace Project
{
    use Library\Registry;
like image 172
lonesomeday Avatar answered Nov 14 '22 22:11

lonesomeday


You need to import your Registry class inside Project namespace, because you need em there, not in global scope.

<?php   
namespace Project
{
    use Library\Registry;

    class Controller
    {
        public $registry;

        function __construct()
        {
            include('E:\Library\Registry.class.php');

            // This is where my trouble is
            // to make it work currently I have to use
            //  $this->registry = new /Library/Registry;
            // But I want to be able to use it like below and that is why
            // I have the `use Library\Registry;` at the top
            $this->registry = new Registry;
        }

        function show()
        {
            $this->registry;
            echo '<br>Registry was ran inside testcontroller.php<br>';
        }
    }
}
?>
like image 36
Timur Avatar answered Nov 15 '22 00:11

Timur