RSS

Override PHP Function

23 Nov

PHP have PECL (PHP Extension & Community Library) function to override built-in functions by replacing them in the symbol table.

bool override_function ( string $function_name , string $function_args , string $function_code )

  1. <?php
  2. override_function(‘strlen’, ‘$string’, ‘return override_strlen($string);’);
  3. function override_strlen($string){
  4. return strlen($string);
  5. }
  6. ?>

The above function “override_function()” require APD i.e. Advanced PHP Debugger.

We can find more about APD here…
http://pecl.php.net/package/apd

Linux users can install apd using below command

# pecl install apd

There is an alternate way to override PHP functions, we can use below class “override” to override any built-in PHP function if PECL is not installed on server 😉

  1. <?php
  2. $or = new override ();
  3. $or->override_function(‘strlen’, ‘override_strlen‘, ‘return override_strlen($string);’);
  4. function override_strlen($string){
  5. return strlen($string);
  6. }
  7. ?>

<?php


$url = 'http://www.svnlabs.com';
$override = new override();

$override->override_function('file_get_contents','fileGetContents',$url);

if ($over_func_name = $override->override_check('file_get_contents')) {
    $result=call_user_func($over_func_name, $url);
}

function fileGetContents($url)
{
  /// statements
}

?>

PHP Function OverRide Class

  1. <?php
  2. class override {
  3. var $functions = array();
  4. var $includes = array();
  5. function override_function($override, $function, $include) {
  6. if ($include) {
  7. $this->includes[$override] = $include;
  8. }
  9. else if (isset($this->includes[$override])) {
  10. unset($this->includes[$override]);
  11. }
  12. $this->functions[$override] = $function;
  13. }
  14. function override_check($override) {
  15. if (isset($this->includes[$override])) {
  16. if (file_exists($this->includes[$override])) {
  17. include_once($this->includes[$override]);
  18. }
  19. if (function_exists($this->functions[$override])) {
  20. return $this->functions[$override];
  21. } else {
  22. return false;
  23. }
  24. } else {
  25. return false;
  26. }
  27. }
  28. }
  29. ?>

Make a habit of creating things modular, that means “pluggable” and “unpluggable”.

<?php
class override {
var $functions = array();
var $includes = array();
function override_function($override, $function, $include) {
if ($include) {
$this->includes[$override] = $include;
}
else if (isset($this->includes[$override])) {
unset($this->includes[$override]);
}
$this->functions[$override] = $function;
}
function override_check($override) {
if (isset($this->includes[$override])) {
if (file_exists($this->includes[$override])) {
include_once($this->includes[$override]);
}
if (function_exists($this->functions[$override])) {
return $this->functions[$override];
} else {
return false;
}
} else {
return false;
}
}
}
?>


Advertisements
 
2 Comments

Posted by on November 23, 2010 in CakePHP, Open Source, PEAR, php, Tips, Tricks

 

Tags: , , , , ,

2 responses to “Override PHP Function

  1. Joe Doe

    December 7, 2010 at 3:59 pm

    Your alternate way to override PHP functions doesn’t work…
    Did you even test it?

    How do you think that could override built-in PHP functions?

     
  2. admin

    December 9, 2010 at 1:04 pm

    Please use…

    $or->override_function(’strlen’, ‘override_strlen‘, ‘return override_strlen($string);’);

    Thanks

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: