Skip to content

Instantly share code, notes, and snippets.

@nicolas-grekas
Last active January 4, 2016 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolas-grekas/8559504 to your computer and use it in GitHub Desktop.
Save nicolas-grekas/8559504 to your computer and use it in GitHub Desktop.
Playing for PPP for native PHP function overloading
git clone https://github.com/nicolas-grekas/Patchwork-PHP-Parser.git ppp
cd ppp
./bin/ppp t1.php #see below for t1.php (and t2.php, required by t1.php)
<?php
function abc($s)
{
return strlen($s) * strlen($s);
}
function def(&$a = 5)
{
$a = 'oups';
return 12;
}
// Patchwork\Shim is not a real function but a parser macro
// that takes the replaced function as first arg
// the replacement function as second
// and the parameters signatures (with optional default value/reference)
// Of course these remplacement are useless and for demo purpose only.
// See https://github.com/nicolas-grekas/Patchwork/blob/master/core/compat/bootup.patchwork.php
// for more useful uses of the feature.
Patchwork\Shim(strlen, abc, $s);
Patchwork\Shim(count, def, &$a = 5);
include "./t2.php";
<?php
$var = 'vvv';
echo strlen($var), "\n";
echo count($var), "\n";
echo $var, "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment