如何在php中重新定义函数?
现在我被 file_exists() 函数困住了。我的项目是从存储库构建的,其中一个作为 DocRoot 进行符号链接,其他项目可通过 include_path 供 php 解释器使用。但默认的 file_exists() 无法查看放置在 include_path 下某处的文件,因此我需要或以某种方式使用包装器重新定义此函数或编写我自己的函数,将其包含在其他模块中并替换目录树中此函数的所有匹配项。这是可能的,但并不好,而且对于开发人员来说也不清楚。
我已经见过 pecl-apd (2008) 和 pecl-runkit (2007),但是在我的 gentoo 存储库或覆盖层中不再有这样的 pecl。那么,如何以现代的方式重新定义函数呢?
==更新===
好的。让我们更深入地了解一下。
假设我们有 /home/me/repos/
目录,其中放置了我们的 git 模块,还有一个虚拟主机 /var/srv/domain.com
,所有内容都在其中组装
repos_
\_site_root_
\_.git
\_file1
\_module1_
\_.git
\_we_need_this_file_too
\_module2_
\_.git
domain.com_
\_htdocs –> ~/repos/site_root
\_tmp
所以我们需要以某种方式将模块 1 和 2 的文件夹包含到项目中。然后通过 vhost 配置将 ~/repos 的路径添加到 php include_path
php_admin_value include_path ".:/home/me/repos"
现在 php 可以
include ('module1/we_need_this_file_too');
从我的 repos 目录中。但是当它尝试在包含 file_exists() 之前检查文件时,它会失败,因为这些目录对于 file_exists() 仍然是关闭的。这就是问题所在。
现在我找到了带有绝对路径的解决方法,无论如何我必须在我的项目中创建个人配置。我也想在需要时将 chdir() 放入 /home/me/repos/
(并描述 vhost 中的
部分),但这很糟糕主意。
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.
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?
== upd ===
Ok. Let's go deeper.
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
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"
Now php can
include ('module1/we_need_this_file_too');
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 PHP 手册
好吧,正如您已经提到的,如果您安装 runkit 扩展 并使用 runkit_function_redefine 或者如果您安装 APD 并使用 override_function。
From the PHP manual
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.
我正在研究 php5.3 中函数重新定义的库
来源:https://github.com/Bubujka/bu。 defun
示例: http://defun.bubujka.org/doc.html
简单重新定义:
I working on library for function redefining in php5.3
Source: https://github.com/Bubujka/bu.defun
Exampls: http://defun.bubujka.org/doc.html
Simple redefining: