In C++ one can do this:
namespace qux = std::foo::bar::baz;
qux::CFoo BAR;
Can one do such a thing in PHP?
You can do this :
namespace foo\bar\baz;
use foo\bar\baz as renamed;
new renamed\cFoo(); // Points to foo\bar\baz\cFoo()
See the documentation for further details.
Namespaces may be aliased (docs).
The general idea is use … as …; as shown below.
use std\foo\bar\baz as qux;
qux\CFoo();
And here's a try-this-at-home example:
<?php
namespace std\foo\bar\baz {
function CFoo() {
echo 'hello, world';
}
}
namespace {
use std\foo\bar\baz as qux;
qux\CFoo();
}
?>
Related
I have been trying to use and understand namespace and traits but getting this error:
"Trait a\b\Train not found" when I run example.php
"Trait a\b\Train not found" when I run Bayes.php
Just confused how it works and why getting error.
Here my code:
(these files store in the same folder)
//example.php
use a\classification;
include_once 'Bayes.php';
$classifier = new Bayes();
$classifier -> train($samples, $labels);
$classifier -> predict([something]);
//Bayes.php
namespace a\classification;
use a\b\Predict;
use a\b\Train;
class Bayes {
use Train, Predict
}
//Train.php
namespace a\b;
trait Train{
}
//Predict.php
namespace a\b;
trait Predict{
}
I'm sorry for my dumb question, really appreciate for your help :"
You first need to include Train.php and Predict.php inside Bayes.php. You're referring to the traits, but how does PHP know about those?
Then, you need to specify a proper namespace when creating a new Bayes(). You include the file, but it's in another namespace. The use declaration at the top isn't doing much. It lets you create a new classification\Bayes() instead of new a\classification\Bayes().
Try something like this instead:
example.php
<?php
use a\classification\Bayes;
include_once 'Bayes.php';
$classifier = new Bayes();
$samples = "text";
$labels = "text";
$classifier -> train($samples, $labels);
$classifier -> predict(["something"]);
Bayes.php
<?php
namespace a\classification;
require_once 'Train.php';
require_once 'Predict.php';
use a\b\Predict as P;
use a\b\Train as T;
class Bayes
{
use T, P;
}
Train.php
<?php
namespace a\b;
trait Train
{
public function Train()
{
echo "training";
}
}
Predict.php
<?php
namespace a\b;
trait Predict
{
function predict()
{
echo "predicting";
}
}
Note the use of an alias in Bayes.php: use a\b\Predict as P; This is where aliases can be helpful to simplify code. If the alias isn't specified, it just uses the last name without namespaces.
You're running into trouble because you're not requiring the files you need, so PHP doesn't know what you're talking about when you ask it to use some of these classes.
example.php requires access to Bayes.php
use a\classification;
require_once 'Bayes.php';
Bayes.php requires access to Train.php and Predict.php
namespace a\classification;
require_once 'Train.php';
require_once 'Predict.php';
use a\b\Predict, a\b\Train;
class Bayes {
use Train, Predict
}
Note: use require_once rather than include_once when the external resource is necessary.
I'm trying to run this code on the same file:
namespace Foo1\Bar\SubBar;
class SubBarClass {
public function __construct() {
echo 'From Foo1';
}
}
namespace Foo2\Bar\SubBar;
class SubBarClass {
public function __construct() {
echo 'From Foo2';
}
}
use Foo1\Bar\SubBar;
$foo1 = new SubBarClass;
use Foo2\Bar\SubBar;
$foo2 = new SubBarClass;
The ideia is to change namespaces and echo the related value.
But it's returning the following error:
( ! ) Fatal error: Cannot use Foo2\Bar\SubBar as SubBar because the name is already in use in C:\wamp\www\xxx\namespaces.php on line 30
Line 30: use Foo2\Bar\SubBar;
How can I interchange namespaces on the same file?
Thks!
use keyword is used to import that namespace to be accessed in your current file scope. It does not act as a namespace "instance constructor".
You're current under Foo2\Bar\SubBar namespace. Like a directory of classes, while you're here, you should access other namespaces from the root (\):
$foo2 = new SubBarClass;
$foo1 = new \Foo1\Bar\SubBar\SubBarClass;
There is no need to use use for those namespaces (although you can, specially when they share parent namespaces), they are already declared in the same file you're using them.
For more information about this, consider reading the manual, where it describes using multiple namespaces in the same file.
This happens because the last defined namespace is the one currently active.
So, when I type:
use Foo1\Bar\SubBar;
I'm still on the last defined namespace: Foo2\Bar\SubBar.
Hence, when I type:
use Foo2\Bar\SubBar;
I'm trying to use the currently active namespace. That's why the Fatal error is returned.
On possible solution is:
namespace Foo1\Bar\SubBar;
class SubBarClass {
public function __construct() {
echo 'From Foo1';
}
}
namespace Foo2\Bar\SubBar;
class SubBarClass {
public function __construct() {
echo 'From Foo2';
}
}
use Foo1\Bar\SubBar;
$foo1 = new SubBar\SubBarClass;
echo '<br>';
$foo2 = new SubBarClass;
Cheers!
If I have a PHP namespace as follows:
<?php
namespace A\B;
$test = new MyClass();
it seems every time I create a new instance of class, the name is prefixed with the namespace, e.g.
$test = new A\B\MyClass();
What happens if I don't want to use a namespace for another class, e.g. I want to call
$test = C\D\AnotherClass();
Currently this becomes:
$test = A\B\C\D\AnotherClass();
which results in an error.
For php7 you can wrap code in a namespace then call it with use in the global namespace declared using namespace with no name per the example below.
namespace FRED\WILMA\BAMBAM {
const FW = 2;
function fw() {
echo "<br>fw<br>";
}
}
namespace BARNEY\BETTY { // Create Namespace and add function
function bb() {
echo "<br>bb<br>";
}
}
namespace { // use global namespace
use FRED\WILMA\BAMBAM;
use BARNEY\BETTY;
BETTY\bb();
BAMBAM\fw();
}
I have a file which looks like this:
<?php
namespace n;
f(); // this calls n\f()
PHP statements can be included by using the include function, however, this doesn't work for the namespace statement:
<?php
include('example_include_namespace_statement.php')
f(); // this doesn't call n\f()
example_include_namespace_statement.php:
<?php
namespace n;
Is there any way to "include" the namespace statement in PHP?
Or is it simply not possible?
You have to specify the namespace when calling a function that is in a namespace, like so:
// test.php
namespace test;
function doesItWork() {
return 'It Works!';
}
// index.php
include 'test.php';
echo test\doesItWork();
If you want to use a function that is defined in a namespace that is outside the current namespace then you must prefix it with the namespace character, like so:
echo \test\doesItWork();
You should read the documentation on namespaces.
I have a file that require()'s a namespace, as such:
<?php
require_once('Beer.php'); // This file contains the Beer namespace
$foo = new Beer\Carlsburg();
?>
I would like to put the Beer namespace directly in the same file, like this (unworking) example:
<?php
namespace Beer {
class Carlsburg {}
}
$foo = new Beer\Carlsburg();
?>
However, the PHP interpreter complains that No code may exist outside of namespace. I can therefore wrap $foo declaration in a namespace, but then I must also wrap Beer in that namespace to access it! Here is a working example of what I am trying to avoid:
<?php
namespace Main\Beer {
class Carlsburg {}
}
namespace Main {
$foo = new Beer\Carlsburg();
}
?>
Is there any way to include the code for the Beer namespace in the file, yet not wrap the $foo declaration in its own namespace (leave it in the global namespace)?
Thanks.
You should use the global namespace :
<?php
namespace Beer {
class Carlsburg {}
}
namespace { // global code
$foo = new Beer\Carlsburg();
}
?>
See here -> http://php.net/manual/en/language.namespaces.definitionmultiple.php
Try this
namespace Beer {
class Carlsburg {}
}
//global scope
namespace {
$foo = new Beer\Carlsburg();
}
As per example #3 in Defining multiple namespaces in the same file
Try placing a backslash before the namespace name:
$beer = new \Beer\Carlsberg();
The initial backslash is translated to "global namespace". If you do not put the leading backslash, the class name is translated to the current namespace.
Just write it, it has no "name":
<?php
namespace Main\Beer {
class Carlsburg {}
}
namespace {
use Main\Beer;
$foo = new Beer\Carlsburg();
}
?>
Demo and see Defining multiple namespaces in the same fileDocs.