PHP

組み込み関数の挙動をdeckorate拡張で置き換える

More than 5 years have passed since last update.

deckorate拡張は

https://github.com/t3rmin4t0r/deckorate

から落とせます。phpizeして実行すると

<?php

use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;

class User
{
private $name = 'john';
private $password;

public function getName()
{
return $this->name;
}
}

if (deckorate("json_encode", '__json_encode')) {
function json_encode($value, $options = 0) {
if (is_object($value)) {
$value = (new ClassMethodsHydrator)->extract($value);
}

return __json_encode($value, $options);
}
}

var_dump(json_encode(new User)); // string(15) "{"name":"john"}"

http://www.php.net/manual/ja/function.runkit-function-redefine.php

に対する利点?? 小さいとか?