Php 如何在命名空间中包装第三方脚本

Php 如何在命名空间中包装第三方脚本,php,namespaces,Php,Namespaces,我有一些无法更新的第三方脚本。一个脚本定义一些全局函数,而另一个脚本使用这些函数 // script 'theirs.php' function SomeFunction(...){} 我的脚本还有一个同名的全局函数 // script 'mine.php' function SomeFunction(...){} 是否可以将代码“包装”到名称空间中 <?php namespace Their\Code; include 'theirs.php' ?> 这样,我的脚本就可以根

我有一些无法更新的第三方脚本。一个脚本定义一些全局函数,而另一个脚本使用这些函数

// script 'theirs.php'
function SomeFunction(...){}
我的脚本还有一个同名的全局函数

// script 'mine.php'
function SomeFunction(...){}
是否可以将代码“包装”到名称空间中

<?php
namespace Their\Code;

include 'theirs.php'
?>
这样,我的脚本就可以根据正常情况调用我的函数,如果需要,还可以调用它们的函数

<?php
// script somefile.php
use Their\Code;

now call their other file that expects their function.
include 'theirotherfile.php';
?>

<?php
// script 'theirotherfile.php'
// that file will be using the namespace Their\Code and not my functions.
SomeFunction(...);
?>
因此,是否可以将函数定义为命名空间的一部分

<?php
namespace Their\Code;

include 'theirs.php'
?>

然后可以使用包含的文件调用“use”。

为什么不为函数使用名称空间?@MarkusMalkusch,你能解释一下你的意思吗?显然,你的\SomeFunction发生了冲突。您必须将其中一个函数移动到另一个命名空间中。由于您对第三方声明没有影响,因此可以为您的声明一个名称空间,例如mine\SomeFunction。