简体   繁体   English

挂钩PHP现有函数以在调用时触发函数

[英]Hook a PHP existing function to trigger a function when called

For MVC reasons, I want to be able to trigger a function to find when the function has been called, since Codeigniter has functions around their core, I want to hook a function such as setcookie and create a file when it's been called (from the triggered function) for example: 出于MVC的原因,我希望能够触发一个函数来查找函数被调用的时间,因为Codeigniter在它的核心周围有函数,我想挂钩一个函数,比如setcookie并在调用它时创建一个文件(来自触发功能)例如:

function call_me()
{
    $file = fopen('setcookie.txt', 'a+');
    fwrite($file, 'Called at ' . __CLASS__);
    fclose();
}

So when setcookie is called, it should trigger the call_me function. 因此,当调用setcookie时,它应该触发call_me函数。 Is there any specific function or method to do this? 有没有具体的功能或方法来做到这一点? I know about debug_backtrace but that's not the purpose I want. 我知道debug_backtrace但这不是我想要的目的。

What you basically need to have a look at is Observers . 您基本上需要了解的是Observers

The observer pattern (aka. Dependents, publish/subscribe) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. 观察者模式 (也称为Dependents,发布/订阅)是一种软件设计模式,其中一个称为主体的对象维护其依赖者的列表,称为观察者,并自动通知他们任何状态变化,通常通过调用其中一个他们的方法。 It is mainly used to implement distributed event handling systems. 它主要用于实现分布式事件处理系统。 Observer is also a key part in the familiar MVC architectural pattern. Observer也是熟悉的MVC架构模式的关键部分。 In fact the observer pattern was first implemented in Smalltalk's MVC based user interface framework. 实际上,观察者模式最初是在Smalltalk基于MVC的用户界面框架中实现的。 1 1

Why don't you try what is described here : 你为什么不尝试这里描述的内容:

http://devzone.zend.com/1384/observer-pattern-in-php/ http://devzone.zend.com/1384/observer-pattern-in-php/

I know about debug_backtrace but that's not the purpose I want. 我知道debug_backtrace,但这不是我想要的目的。

I see that you insist not to use backtrack function, but still I believe that when you want to log when a function is called backtrack can come in handy. 我看到你坚持不使用回溯功能,但我仍然相信当你想要记录功能时,回溯可以派上用场。

The idea is that you have a predifined piece pf code stored in a constant, whenever you want to debug an if condition evaluates this code. 这个想法是你有一个存储在常量中的预定义代码,只要你想调试if条件评估这个代码。

If you are under prouction the if statement will prevent from evaluating anything so your code's speed is not affected. 如果您处于原状,if语句将阻止评估任何内容,因此您的代码速度不会受到影响。 If it works for you you can modify it to your needs, to track down even more levels. 如果它适合您,您可以根据需要进行修改,以追踪更多级别。

To make my point this is a full example, if I have not understood right and this is not what you're looking for, my apologies! 为了说明我的观点,这是一个完整的例子,如果我不理解,这不是你想要的,我道歉!

To check the example you have a file: test.php 要检查示例,您有一个文件: test.php

<?php

define ('__SITE_PATH',realpath(dirname(__FILE__)).'/');  
ini_set('log_errors', 1); 
ini_set('error_log',  __SITE_PATH.'my_error_log.log');   
include 'test1.php';
include 'test2.php';


define (__DEBUG_EVAL, ' 
    $dbt = debug_backtrace();
    error_log(
    "\n".
    "Parent function file: "         . $dbt[1]["file"]      . "\n" .
    "Parent function class: "        . $dbt[2]["class"]     . "\n" .        
    "Parent fiunction name: "        . $dbt[2]["function"]  . "\n" .    
    "Par. fiunc. called from line: " . $dbt[2]["line"]      . "\n" .

    "Child function file: "           . $dbt[0]["file"]     . "\n" .
    "Child function class: "          . $dbt[1]["class"]    . "\n" .        
    "Child fiunction name: "          . $dbt[1]["function"] . "\n" .    
    "Child fiunc. called from line: " . $dbt[1]["line"]     . "\n" .
    "\n"
    ); 
    ');  


test1::a(); 

   ?>

This is test1.php 这是test1.php

<?PHP

class test1
{
    public static function a()
    {
        test2::b();
    }
}

?>

The last is test2.php 最后一个是test2.php

<?PHP



class test2
{
    public static function b()
    {
        if(defined('__DEBUG_EVAL')) eval(__DEBUG_EVAL);   
        echo 'Hello!';
    }   
}

?>

This is the result : 这是结果

[13-Apr-2012 14:37:18] 
Parent function file: C:\PHP-GTK\MyProjects\Electre\test1.php
Parent function class: test1
Parent fiunction name: a
Par. fiunc. called from line: 29
Child function file: C:\PHP-GTK\MyProjects\Electre\test2.php
Child function class: test2
Child fiunction name: b
Child fiunc. called from line: 7

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

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