php - 临时命名空间

标签 php namespaces

有什么办法可以临时使用命名空间吗? 我正在使用一个库来创建表单,它使用命名空间,问题是我通常想在页面中间创建一个表单,因此位于全局命名空间中。然后,如果我想调用这个库的任何函数,我必须在所有内容前面加上命名空间\

PHP 中没有办法做这样的事情吗:

Blabla global namespace
strlen('test'); // 4

namespace Namespace
{
    test();
}

More global PHP

让它引用命名空间\test?

最佳答案

http://www.php.net/manual/en/language.namespaces.importing.php

<?php
namespace foo;
use My\Full\Classname as Another;

// this is the same as use My\Full\NSname as NSname
use My\Full\NSname;

// importing a global class
use ArrayObject;

$obj = new namespace\Another; // instantiates object of class foo\Another
$obj = new Another; // instantiates object of class My\Full\Classname
NSname\subns\func(); // calls function My\Full\NSname\subns\func
$a = new ArrayObject(array(1)); // instantiates object of class ArrayObject
// without the "use ArrayObject" we would instantiate an object of class foo\ArrayObject
?>

这大约是您能得到的最接近的结果 - 无法暂时更改默认命名空间。

关于php - 临时命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9969829/

相关文章:

asp.net - 从 aspx 页面注入(inject)代码的方法

php - 我应该在哪里使用 'use' 在 php 中?

php - 使用 PHP 抓取推文

php - password_verify 如何在不知道盐和成本的情况下验证密码?

php - 错误 1045 : access denied for user 'root' @'localhost' using password NO. .. 通过扩展类连接时,其他

javascript - 无法设置未定义的属性 - JavaScript 中的全局对象和命名空间

php - 用 GD 绘制图形

php - 拉拉维尔 5 : Where define global Vars?

c++ - 如果类在命名空间下,则友元函数无法访问私有(private)函数

C# 找不到 namespace 名称