How to redefine a function in php?

∥☆過路亽.° 提交于 2019-12-11 09:20:09

问题


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.


回答1:


From the PHP manual

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

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.




回答2:


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:

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

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

---

Waserd
Bubujka


来源:https://stackoverflow.com/questions/6925736/how-to-redefine-a-function-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!