English 中文(简体)
网址上的mon干patch
原标题:monkey patching in php

我试图说明key子的配送工作如何,以及我如何用自己的物品/方法开展工作。

I ve been looking at this lib, it does exactly what I want to do myself: https://github.com/antecedent/patchwork

With it you can redefine a method from an object. It uses the monkey patch technique for that. But I couldn t really figure out what exactly is going on by looking at the source.

我有以下目标:

//file: MyClass.php
namespace MyClass;

class MyClass {

    public function say()
    {
        echo  Hi ;
    }
}

我这样说:

Monkeypatch
eplace( MyClass ,  say , function() {
    echo  Hello ;
});

$obj = new MyClass();
$obj->say();  // Prints:  Hello 

但i m不能确定如何规范实际派遣部分。 我知道这方面的名称空间很重要。 但是,这究竟如何让我拿出某种方法? 我是否还需要在某个地方(如果是,如何)使用电子数据?

I couldn t really find any good examples about this matter, except: http://till.klampaeckel.de/blog/archives/105-Monkey-patching-in-PHP.html

但我确实看不出我如何能够将这一点适用于我自己的物体/方法。 我希望得到很好的解释或榜样。

最佳回答

http://till.klampaeckel.de/blog/archives/105-Monkey-suing-in-PHP.html” rel=“nofollow”http://till.klampaeckel.de/blog/archives/105-Monkey-TOPing-in-PHP.html 实际产生差异的是第二个透镜前使用的特性。

当你使用名称空间时,你可以直接使用名称空间宣布的方法/类别:<><>> > > >。

use TheNamespace;
$var = new TheClass();

或者通过使用诸如:

<代码>$var = 新的NamespaceTheClass();

因此,通过援引<代码>strlen(),而不是strlen(,你明确要求PHP使用默认透镜,而不是为这一名称空间界定的透镜。

关于mon子的配送,您可使用 run基(。 另外,关于铺设工作,网站有相当数量的范例(rel=“nofollow”http://antecedent.github.com/TOPwork/docs/examples.html)。 你们可以检查替代某类功能的魔法。

问题回答

您可使用runkit进行时间级修改。 更具体地说,您可使用runkit_method_redefine

截至PHP5.6,仍没有支持mon干的配送;然而PHP5.3则引入了匿名功能。 答案并不是你回顾什么,或许可以改进,但一般想法是使用阵列,anonymous function<<,以建立自成一体、自成一体的自我参照阵列(如果你愿意的话):

test.php

$inner = require( test2.php );
$inner[ say ](); // Hi!

$inner[ data ][ say ] =  Bye! ;
$inner[ say ](); // still says Hi!

$inner[ set_say ]( Bye! );
$inner[ say ](); // Bye!

$inner = require( test2.php );
$inner[ say ](); // Hi!

test2.php

$class = array(
     data  => array(
         say  =>  Hi! 
    ),

     say  => function() use (&$class){
        echo $class[ data ][ say ]. <br /> ;
    },

     set_say  => function($msg) use (&$class){
        $class[ data ][ say ] =& $msg; 
    }
);

return $class;

另外, 哪里有一个否认者说,上述法典(加上PHP的mon子配送)几乎总是是一个可怕的想法,但有时是绝对必要的。

I have monkey-patched a class using eval() and namespaces. This may not be the answer for you, though, since this does not work if the class you are monkey-patching is already in a namespace. I haven t figured out how to get around that, other than trimming the namespace declaration from the eval string. However, doing so would likely break any namespace-dependent code within the class methods.

就我的情况而言,我正在把PDO的核心班子围起来,以便联合测试一个取决于数据库互动的班子。 但是,也许看到我的手法将有助于你了解如何为你的处境工作。

I have the code snippets up in a blog post here: http://chrisgriffing.com/coding/php/2012/04/12/how-to-mock-pdo-and-other-objects/

也许你已经看到这一点,但仅供参考的是,他们正在使用流包装器,

rel=“nofollow” http://php.net/manual/es/Function.stream-wrapper-register.php

基本上,他们在档案和捕鲸上登记一个流包,因此,当该代码被装满时,ay可以加以操纵。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签