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());
Related
I can not understand why php gives me an error
"Fatal error: Cannot declare class rex\builder\RexBuilder, because the
name is already in use in /var/www/site2.dev/App/rex/RexBuilder.php on
line 12"
RexBuilder static class, and it is called only 1 time.
I did a search on the project, no longer classes with the same name.
<?php
namespace rex\builder;
require_once 'Router.php';
use rex\router\Router;
error_reporting(E_ALL);
ini_set('display_errors', 1);
class RexBuilder {
public static function collector($array) {
$router = new Router();
foreach ($array as $key => $val) {
$router->get($val->getMethod(), $val->getInterfaces(), $val->getHandler());
}
$router->init();
}
}
?>
Call the class in index.php
RexBuilder::collector(array(
new BuildModel('POST', '/api/v1/user/register', new \api\register\Registration()),
new BuildModel('POST', '/api/v1/user/login', new \api\login\Login())));
More This class is not used
The error is thrown because of the use rex\router\Router; duplicate classes.
When you are writing use namespace.. it means you can go directly to that namespace like it is your current namespace
Lets take a look at the next code:
We'll create a file and declare that it belongs to namespace classes\a
//file: a.php
<?php
namespace classes\a;
class A{
}
now lets create another file b.php (and declare it belongs to namespace classes\b but it means nothing for the example)
namespace classes\b;
require_once "a.php";
use classes\a; //Notice that I'm using this namespace, it means I can use it directly
class A{
}
Generates the error
Fatal error: Cannot declare class classes\b\A because the name is already in use in
We have to solutions possible:
First: remove the use tag and write the namespace directly
class A{
function __constructor(){
$instance = new classes\a\A();
}
}
Second, give it alias
use classes\a as out_a;
class A{
function __constructor(){
$instance = new out_a\A();
}
}
For your code, just remove the use or give it an alias.
The problem is certainly because you include the RexBuilder.php file two times instead of one.
If you call the file by this way : include('RexBuilder.php'); or this way require('RexBuilder.php'); please change it by include_once('RexBuilder.php'); or require_once('RexBuilder.php'); which only allows ONE call of the file.
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.
Is it possible to use a classes methods without actually calling the class into a variable. I am sure i have seen this somewhere but i'm not sure if i was dreaming.
Take this example:
<?php
namespace proj;
class beer{
public function whichIsBest(){
return 'Not cheap stuff';
}
}
Include the file start the class but then how can i get to the whishIsBest method without calling the class into a variable first.
<?php
include 'beerClass.php';
new \proj\beer();
echo \proj\beer()->whichIsBest
Or is this just not possible and I was actually dreaming?
http://www.php.net/manual/en/language.oop5.static.php
class beer {
public static function whichIsBest() {
do //
}
}
..
echo beer::whichIsBest();
This is an experiment with PHP namespaces / autoload in a single file.
namespace trust;
class trust_network{
public function __construct(){
print "SUP";
}
}
namespace trust2;
$trust = new \trust\trust_network(); $do = new \test();
function __autoload($class){
require($class.".php");
print $class;
}
So under namespace trust2, I'm calling "\test" - aka I'd like to autoload that class from an external file on a global base. What I wrote does not work. I know that I've got __autoload under a namespace, but how do I declare that on a global basis? Can't include before namespace declaration.
For multiple namespaces in one file you should use the curly bracket syntax:
namespace n1 {
...
}
namespace n2 {
...
}
namespace {
...
}
In the last block you can declare functions in the global namespace. Reference: http://www.php.net/manual/en/language.namespaces.definitionmultiple.php
Autoload is usually so that you can put one class per file. Therefore, you should have the following layout
/index.php
function __autoload($class){
// You may need to convert backslashes in $class to forward slashes
// and strip the first slash, we'll leave the
require($class.".php");
// debug-only: print $class;
}
// Calling new here triggers __autoload to be called
$trust = new \trust\trust_network();
$do = new \test();
/trust/trust_network.php
namespace trust;
class trust_network{
public function __construct(){
print "TRUST_NETWORK";
}
}
/test.php
class test() {
public function __construct(){
print "TEST";
}
}
Note that you should use spl_autoload_register instead since it allows multiple systems to hook in their own autoload behavior. As of PHP 5.3, you can do the following
spl_autoload_register(function ($class) {
require($class.".php");
});
I have a unique situation that is hard to debug.
I need to set a global string inside a php class that is not strictly defined as a global, the class must be in another file.
The file.php with the string has simply this:
//this cannot be changed
$foo_version = '1.1.1';
The example.php file trying to access this string must use a class:
class Bar extends Task {
public function main() {
require_once('../file.php');
//global $foo_version; this doesn't work
// update the database with this string, does not work
update_option( 'db_field', $foo_version );
}
}
How can I get the $foo_version to return something inside the class?
Also nothing can be defined/done outside the class.
If you include the file, and the variable is in the include file, you can just use it.
class Bar extends Task
{
public function main() {
require_once('../file.php');
update_option( 'db_field', $foo_version );
}
}
What you have is a variable $foo_version and it can easily be changed. You can use define to make it a constant instead see PHP DOC
define("FOO_VERSION", "1.1.1");
class Bar extends Task {
public function main() {
require_once ('../file.php');
update_option('db_field', FOO_VERSION);
}
}
If you insist Replace require_once with require your code would work because PHP will check if the file has already been included, and if so, not include (require) it again.
If you have used this in another class definitely it would not work. Change your code to the following
class Bar extends Task {
public function main() {
require ('../file.php');
update_option('db_field', $foo_version);
}
}