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

时间:2014-10-20 15:09:25

标签: php namespaces

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

// 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'
?>

然后就可以打电话&#39;使用&#39;包含文件。

0 个答案:

没有答案