Can the namespace statement be "included" in PHP? - php

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.

Related

How do I import all functions in a file/name space?

I have a file
<?php namespace a1;
function a(){}
function b(){}
function c(){}
In another file, I would like to import all the functions from the first file as if they ware in the same namespace. Right now, I have to do something like this:
<php
include 'file1.php';
use a1\{a,b,c};
I want this to be more dynamic as I keep adding functions to file 1 which is part of my bootstrap files.
Better do it in this way:
<?php namespace a1;
class func {
static function a(){}
static function b(){}
static function c(){}
}
and
<?php
include 'file1.php';#better register an autoloader
use a1\func as f;
f::a();
So know you can add more and more mehtod, but there is no need for any update of use.
And you can just wrap class func {} around your functions and add to all static and you are done.
You may use the parent namespace:
namespace a1 {
function a(){echo 'yello!';}
function b(){}
function c(){}
}
namespace a2 {
use a1;
echo a1\a();
}
Output :
yello!
for you example :
<php
include 'file1.php';
use a1;
// call any function under the namespace a1;
var_dump(a1\a());

Namespace, Trait, and Use in PHP

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.

Overriding PHP namespace for a specific class

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();
}

PHP: Namespaces in a single file with a global namespace

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.

Can I rename a namespace in PHP?

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();
}
?>

Categories