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.
Related
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).
/Core/Api.php
<?php namespace Core;
class Api
{
//
}
start.php
<?php
include 'vendor/autoload.php'
index.php
<?php
include 'start.php';
use Core\Api as Api;
new Api // it's work
start.php
<?php
include 'vendor/autoload.php'
use Core\Api as Api;
index.php
<?php
include 'start.php';
new Api; // Fatal error: Class 'Api' not found
There are many tool class will be used in many places, how to alias it as once in some file and let other file can use the aliased name directly?
use works inside current namespace (for a whole file in most cases) only.
You could create empty class to inheritance target class.
<?php
class Api extends \Core\Api
{}
I have a base abstract class, say Base.php located at /lib/Helper/Base.php. Now, I have another class, say Awesome.php located at /lib/Helper/Awesome/Awesome.php, and this class needs to extend Base.
I have defined my namespaces as follows:
Base.php
<?php namespace Helper;
abstract class Base
{
}
Awesome.php
<?php namespace Helper\Awesome;
class Awesome extends Base
{
}
Right now, this says that class Base was not found. I tried to use require_once and/or include to the path of Base.php and they also didn't work. What am I doing wrong?
You are including the file, and that is the right thing to do. But you are also using
namespaces.
the namespace is not a folder, that means the namespace 'Helper' is not a folder but a logical hierarchy. In order to use the class Base in he logical namespace Helper the Awesome class needs to extend the Base class with the full logical path i.e. \Helper\Base.
namespace Helper\Awesome;
class Awesome extends \Helper\Base {
}
or use the use/as keyword instead (like c# using statement) .
namespace Helper\Awesome;
use \Helper\Base as Base;
class Awesome extends Base
{
}
more information can be fount at the php.net site.
here is a direct reference: http://php.net/manual/en/language.namespaces.importing.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.
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.