Cannot use a class with names in another namespace in PHP

I'm still having problems with PHP5 namespaces.

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

registry class is inside the namespace library

This should work, but it is not, 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 use this instead ...

 $this->registry = new Registry; 

This is the reason for using

 use Library\Registry; 

at the top of the Project namespace file


Below I have 3 small sample scripts in a folder structure like this. Library/registry.class.php class in my library folder
Controller/controller.class.php and class in my controller directory
Controller/testing.php 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'; } } } ?> 

File E: \ Controller \ Controller.class.php

 <?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>'; } } } ?> 

File E: \ Controller \ testing.php

 <?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 

if i don't use it below in controller.class.php file

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

Because in this file at the top, I use Library\Registry; I must have access to it like this ...

 $this->registry = new Registry; 

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

+4
source share
4 answers
 use Library\Registry; namespace Project { 

I believe the wrong way: you use ing Library\Registry in the global namespace, and then open the Project namespace.

Place the use statement in the namespace into which you want to import it.

 namespace Project { use Library\Registry; 
+5
source

You need to import your Registry class inside the Project namespace, because you need them there, and not in the 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>'; } } } ?> 
+2
source

Just add: using \ Library \ Registry;

at the top of your script in the namespace declaration

Then you can just say:

$ registry = new registry;

inside your class

By the way, the declaration of your class is incorrect. You should not wrap your namespace inside curly braces, the namespace is not a function.

This is how it should be. Also make sure that the declaration of the Library \ Registry class is already included either by using include ('/path/to/registry.php'); or using autoloader

 namespace Project; 

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

 use \Library\Registry; class Controller { public $registry; function __construct() { // 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>'; } } 

Enjoy

+1
source
 <?php namespace Project; require_once 'your registry class file'; use \Library\Registry as Registry; 

Now you can use ...

  $this->registry = new Registry; 
0
source

Source: https://habr.com/ru/post/1387743/


All Articles