简体   繁体   English

如何在php中重新定义函数?

[英]How to redefine a function in php?

Now i'm stuck with file_exists() function. 现在,我被file_exists()函数所困扰。 My project is built from repositories, one is symlinked as a DocRoot, others are available to php interpreter via include_path. 我的项目是从存储库构建的,其中一个作为DocRoot被符号链接,其他的则可以通过include_path用于php解释器。 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. 但是默认的file_exists()无法查看文件放在include_path下的某个位置,因此我需要或以某种方式用包装器重新定义此函子,或者编写自己的函子,将其包含在其他模块中,并在目录树中替换此函子的所有匹配项。 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. 我已经看过pecl-apd(2008)和pecl-runkit(2007),但是在gentoo存储库或覆盖图中的其他pecls都没有了。 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 假设我们有/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

so we need to somehow include folders with modules 1 and 2 into the project. 因此我们需要以某种方式将包含模块1和2的文件夹包含到项目中。 Then adding path to ~/repos to php include_path via vhost config 然后通过vhost config将〜/ repos的路径添加到php include_path

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

Now php can 现在php可以

include ('module1/we_need_this_file_too');

from my repos directory. 从我的repos目录。 but when it tries to check the file before including with file_exists(), it fails, because those directories are still closed for file_exists(). 但是当它尝试在包含在file_exists()中之前检查文件时,它会失败,因为这些目录仍为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. 我也需要在需要时将chdir()放入/home/me/repos/ (并在vhost中描述<Directory>部分),但这不是一个好主意。

From the PHP manual PHP手册

PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions. PHP不支持函数重载,也无法取消定义或重新定义先前声明的函数。

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 . 好吧,正如您已经提到的,如果您安装了runkit扩展并使用runkit_function_redefine,或者如果您安装了APD并使用了override_function,则可以

I working on library for function redefining in php5.3 我在库中工作以在php5.3中重新定义函数

Source: https://github.com/Bubujka/bu.defun 资料来源: https : //github.com/Bubujka/bu.defun

Exampls: http://defun.bubujka.org/doc.html 示例: http ://defun.bubujka.org/doc.html

Simple redefining: 简单重新定义:

<?php
def('name', function(){ 
    echo "Waserd"; 
});
name();
echo "\n";

def('name', function(){ 
    echo "Bubujka"; 
});
name();

---

Waserd
Bubujka

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM