开发者

How to redefine a function in php?

开发者 https://www.devze.com 2023-03-25 03:19 出处:网络
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(

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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号