Add global namespace to psr4 autoloader - php

I am currently doing something like this:
$loader->addPsr4('Application\\Config\\', __DIR__ . '/../config/test');
This means I can do any file in the directory test with namespace Application\Config\.
Is it possible to a similar setup/trick to do this for the global namespace?

You can do:
$loader->addClassMap(['src\\']);
and this will get you all the files in directory src (no matter what namespace there will be).

Related

PHP do include inherit namespace

Assume the index.php looks like the below:
namespace testing;
include __DIR__ . 'anotherFile.php'
My question is do anotherFile.php inherit the namespace namespace testing;?
A php-file that is included with include or require does not inherit any namespace from the outside. If a namespace is defined in this file, then this applies. If no namespace is defined, the file works in the global namespace.

Using namespace and use in php/laravel?

I have two files in the same directory Graph:
IModel.php
DataModel.php
For these files in the top I set namespace as: namespace App\Library\Graph;
When I try to use IModel.php in DataModel.php I do:
namespace App\Library\Graph;
use IModel;
I get response: Interface 'IModel' not found
You need to include the php file and then you will be able to call the class.
require('App\Library\Graph\IModel.php');
$myClass = new IModel();
You can also use an autoloader.. Then include the autoloader and all your classes that are mapped through the autoloader will be able to be called.
Here is a link to read about autoloading using composer.
https://phpenthusiast.com/blog/how-to-autoload-with-composer

Don't understand php namespace, how can I call function from another class?

I am trying to use Azure storage for php, the setup steps are to include the namespace, include composer autoload and then use the azure classes, so I have the following. However further down I use the class Microgrid and it's not found because of the namespace, it is in a different directory. How can I use other classes that aren't part of that namespace? Also, what's the correct way to specify your namespace path? It's in a different directory relative to the page this is for and the one I am using is not at the root, should I start at the root?
namespace MicrosoftAzure\Storage;
use \MicrosoftAzure\Storage\Common\ServicesBuilder;
use \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions;
use \MicrosoftAzure\Storage\Blob\Models\PublicAccessType;
use \MicrosoftAzure\Storage\Common\ServiceException;
require_once '/var/www/html/vendor/autoload.php';
$action = MicroGrid::GetParameter('action');
ClassNames are considered relative to the current namespace unless they start with a \
This means that inside the MicrosoftAzure\Storage namespace you can use the relative namespace for class.
If you want to call a class from a different namespace you should call fully-qualified name for it like
$action = \MicrosoftAzure\WhereEver\MicroGrid::GetParameter('action');
or use the name space or unique class with fully-qualified name
use \MicrosoftAzure\WhereEver;
or
use \MicrosoftAzure\WhereEver\MicroGrid;
then:
$action = MicroGrid::GetParameter('action');
Edited to make it clear
namespaces allow us to avoid naming collisions and to alias long
names caused by avoiding naming collisions.
it depends to your autoloader for a simple example I create a new project and make this autoloader in the index.php located at root directory
function __autoload($className){
//if it's a full name with windows style slashes correct the path
$file_name = str_replace('\\', '/', $className);
require_once("vender/src/".$file_name.".php");
}
when I call $date = new \App\Utility\Date(); autoloader will require this file:
verdor/src/App/Utility/Date.php
and in Date.php I used this name space namespace App\Utility;
PHP does not provides a class autoloader out of the box.
There are many autoloaders for PHP, and the most common autoloader standard is the PSR-4, used by many frameworks and applications.
If you are not using an autoloader, you should require every class file (and recursive dependencies) before using it.
Azure uses Composer Autoloader and PSR-4.
You should use Composer Autoloader on your project, then import your class from the right namespace (you are not importing it on your example code)
A namespace basically groups your classes together. You can use something outside your namespace by explicitly using it e.g.
namespace App;
use Illuminate\Database\Eloquent\Model;
In this case I'm 'grouping' this and other classes in a namespace called 'App' but I want to use 'Model' provided by Eloquent (the Eloquent class having a namespace of 'Illuminate\Database\Eloquent') in this class.
If 'Microgrid' is not part of your current namespace, you'll need to explicitly add its namespace in your 'use' statements.

PHP Namespace will not change after include or require

actually my question is very simple. In PHP I can define namespaces in files which will be loaded with require or include.
Example:
content of index.php
namespace test;
require('example.php');
die(__NAMESPACE__);
content of example.php
namespace example;
echo "hello world";
If you execute this code, the output will be "test", because the namespace will not change if you include a file with another namespace.
But could you tell me why? How can I imagine the parser?
It could look like:
namespace test{}
namespace example{}
// and here it goes on with test, but why I didnĀ“t said namespace test
namespace test{}
In a PHP File, all code after a namespace declaration will belong to that namespace. This includes the require statements.
Each PHP file has within it its own value for __NAMESPACE__ that is populated when namespace is declared. To access the namespace of another file, you could create a method in example.php that returns __NAMESPACE__. See this question.
Alternately, you could use get_current_namespaces() but this won't tell you what namespace belongs to what file.

Doctrine Autoloader appending namespace to include path

I have the following folder setup
/common
/classes
-sharedClass.php
/project
/classes
-coreClass.php
index.php
bootstrap.php
In my bootstrap.php I'm setting up a namespace
$autoloader = new ClassLoader('CommonFiles', dirname(__DIR__) . '/common/classes/');
$autoloader->register();
In index.php:
require_once 'classes/coreClass.php'
$core = new CoreClass();
in coreClass():
use CommonFiles\SharedClass;
//Other code
I'm getting an error that sharedClass.php can't be found, and the actual namespace is being appended to the file path being checked so that it's:
c:/web/common/classes/CommonFiles/sharedClass.php
So... how do I stop the autoloader from including the namespace as part of the path root it's looking for?
So after some closer review apparently this is just how autoloading works. I reviewed some of the baked in Doctrine samples and it looks like the namespace is appended automatically to all your use statements. This wasn't just a fluke but a design decision and accounting for it meant an easy fix.

Categories