PHP Namespace will not change after include or require - php

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.

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

Is it correct to use PHP namespace declaration in 2 separate scripts?

I'm learning about PHP namespaces. I'm attempting to add a namespace to a class I wrote, and then reference it in another script. I've done it like this:
MenuBuilder.php
namespace Andytest\MenuBuilder;
class MenuBuilder {
public function test() {
echo 'testing';
}
}
test.php (where the class is used)
namespace Andytest\MenuBuilder;
require_once 'MenuBuilder.php';
$Builder = new MenuBuilder;
$Builder->test();
The output from this is as I expect - it outputs the word 'testing' when I run test.php.
But I'm not sure why I need namespace Andytest\MenuBuilder in test.php because it's already decleared in MenuBuilder.php, which is being required by test.php? If I remove the namespace line in test.php it doesn't work.
Am I doing this correctly?
In test.php you should have use Andytest\MenuBuilder\MenuBuilder; instead of namespace Andytest\MenuBuilder;
Because you use namespace instead of use the file expects every class you create that is not specified in the use case to be in the same namespace, therefore new MenuBuilder(); actually calls new Andytest\MenuBuilder\MenuBuilder();.
If you would change the namespace in test.php it should tell you that it cant find MenuBuilder

PHP namespace and include files

I have the following PHP code:
<?php
namespace my_ns;
class dummy
{
function do_print(){echo "printing...";}
}
$obj_dummy=new dummy();
$obj_dummy->do_print();
?>
This works all fine,
How ever If I put the class in a separate include file (e.g. class.dummy.php), making the code on the page look like below:
<?php
namespace my_ns;
include ("class.dummy.php");
$obj_dummy=new dummy();
$obj_dummy->do_print();
?>
I get the error message:
Class 'my_ns\dummy' not found in ...
How can by default make sure that (all) include-files are automatically added to a given namespace?
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.
Source
Add this to your included file as well.
namespace my_ns;
After that, your code works just fine.
Reference

PHP Name spacing problem with extends

I have a file that declares a namespace in the beginning, then does an include of two supporting files that defines classes like X, Y, etc.
Now in the main file, after declaring the namespace, I can no longer create classes that extend X. I have not declared a namespace in X or Y, I assumed the definition at the top of the main file, before the includes, would take care of that? Shouldn't the class just default resolve to my namespace\X?
For example, in my PHP file I do this in the beginning:
namespace SGOAuth;
include 'OAuth.php';
include 'CURL.php';
And later a class I define in this file tries:
class MyClass extends CURL {...}
But I get the error: SGOAuth\CURL not found
Thanks!
Unless the CURL class is declared in the namespace SGOAuth, of course it won't exist in the namespace SGOAuth. Just including a file doesn't mean it's part of the namespace the file that includes the file is in (now that's a sentence ;)). That would make namespaces pointless.
CURL.php
// no namespace declaration, defaults to global namespace
class CURL { }
foo.php
// namespace declaration, all code *in this file* is in namespace Foo
namespace Foo;
// includes the CURL class, which is in the global namespace
include 'CURL.php';
new CURL; // error, class does not exist in this namespace
new \CURL; // works
So class CURL by default is in the global namespace. To extend it, you need to extends \CURL.

Categories