如何在php中重新定义函数? [英] How to redefine a function in php?

查看:296
本文介绍了如何在php中重新定义函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我陷入了file_exists()函数的困境.我的项目是从存储库构建的,其中一个作为DocRoot被符号链接,其他的则可以通过include_path供php解释器使用.但是默认的file_exists()无法查看文件是否位于include_path下的某个位置,因此我需要或以某种方式用包装器重新定义此func或编写自己的函数,将其包含在其他模块中,并在目录树中替换此func的所有匹配项.可能,但是不好,对开发人员来说还不清楚.

Now i'm stuck with file_exists() function. My project is built from repositories, one is symlinked as a DocRoot, others are available to php interpreter via include_path. But the default file_exists() cannot look at the files are placed somewhere under include_path, so i need or somehow redefine this func with a wrapper or to write my own, include it in other modules and replace all matches of this func in the directory tree. It's possible, but is not good and is not clear for developers.

我已经看过pecl-apd(2008)和pecl-runkit(2007),但是在gentoo存储库或叠加图中,不再有这样的pecls.那么,如何以现代方式重新定义功能?

I've already seen pecl-apd (2008) and pecl-runkit (2007), but there are no more such pecls among the others in my gentoo repository or the overlays. So, how to redefine a function in a modern way?

==更新===

好的.让我们更深入.

假设我们有/home/me/repos/目录,其中放置了我们的git模块,还有一个虚拟主机/var/srv/domain.com,其中所有程序都已组装

Assume we have /home/me/repos/ directory, where our git modules are placed and a virtual host /var/srv/domain.com, where it all is assembled

repos_
      \_site_root_
                  \_.git
                   \_file1
       \_module1_
                 \_.git
                  \_we_need_this_file_too
        \_module2_
                  \_.git
domain.com_
           \_htdocs –> ~/repos/site_root
            \_tmp

所以我们需要以某种方式将包含模块1和2的文件夹包含到项目中.然后通过vhost config将〜/repos的路径添加到php include_path中

so we need to somehow include folders with modules 1 and 2 into the project. Then adding path to ~/repos to php include_path via vhost config

php_admin_value include_path ".:/home/me/repos"

现在php可以了

include ('module1/we_need_this_file_too');

从我的repos目录中.但是当它尝试在包含file_exists()之前检查文件时,它失败了,因为那些目录对于file_exists()仍然关闭..这就是问题所在.

from my repos directory. but when it tries to check the file before including with file_exists(), it fails, because those directories are still closed for file_exists(). This is the problem.

现在,我已经找到了使用绝对路径的解决方法,无论如何,我必须在我的项目中创建个人配置.我也想在需要时将chdir()放入/home/me/repos/(并在vhost中描述<Directory>部分),但这不是一个好主意.

Now i've found a workaround with absolute paths, anyway i'd have to create a personal config in my project. I also though about to chdir() into /home/me/repos/ when it needs to (and describing <Directory> section in vhost), but it was bad idea.

推荐答案

来自

PHP不支持函数重载,也无法取消定义或重新定义先前声明的函数.

PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.

好吧,正如您已经提到的,如果您安装 runkit扩展,则可以并使用 runkit_function_redefine 或安装 APD 并使用

Well, you may, as you already mentioned, if you install the runkit extension and use runkit_function_redefine or if you install APD and use override_function.

这篇关于如何在php中重新定义函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆