Redefine Built in PHP Functions

Asked
Active3 hr before
Viewed126 times

7 Answers

built
90%

Note: By default, only userspace functions may be removed, renamed, or modified. In order to override internal functions, you must enable the runkit.internal_override setting in php.ini., 4 Editing the php.ini file will affect all PHP scripts running on that particular installation of PHP. The function redifinition itself, however, is exclusive to the scripts that call runkit_function_redefine(). – Johannes Gorset Feb 24 '10 at 14:58 ,Patchwork is a PHP library that makes it possible to redefine user-defined functions and methods at runtime, loosely replicating the functionality runkit_function_redefine in pure PHP 5.3 code, which, among other things, enables you to replace static and private methods with test doubles.,Having to redefine native PHP functions or language statements should ring an alarm bell though. This should not be part of your production code in my opinion, unless you are writing a debugger or similar tool.

Personally I'm using this to mock e.g. time() for unit test. I published those mocks in the library PHP-Mock:

namespace foo;

use phpmock\ phpunit\ PHPMock;

class FooTest extends\ PHPUnit_Framework_TestCase {

   use PHPMock;

   public
   function testBar() {
      $time = $this - > getFunctionMock(__NAMESPACE__, "time");
      $time - > expects($this - > once()) - > willReturn(3);
      $this - > assertEquals(3, time());
   }
}
88%

a.create new file with custom namespace,
   for example ovr.php
b.define all
function that neeed to be overrided
c.include ovr.php at the beginning of each file, add namespace to that file to use same namespace used by ovr.php

   //------------this is ovr.php
   <
   ? php declare(strict_types = 1);
namespace dk;
/**
 * override parse_url return empty string despite null
 * 
 * @param string $url
 * @param int $component
 * @return mixed
 */
function parse_url(string $url, int $component = -1): mixed {
   $ret = \parse_url($url, $component);
   if (is_null($ret)) $ret = '';
   return $ret;
}

//------------this is other-file.php
<
? php declare(strict_types = 1);
namespace dk;

$a = parse_url('/apath', PHP_URL_HOST);
var_dump($a);
72%

Any valid PHP code may appear inside a function, even other functions and class definitions. , All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa. , PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions. ,Note: Function names are case-insensitive for the ASCII characters A to Z, though it is usually good form to call functions as they appear in their declaration.

<
? phpfunction foo($arg_1, $arg_2, /* ..., */ $arg_n) {
   echo "Example function.\n";
   return $retval;
} ? >
load more v
65%

A function is a block of statements that can be used repeatedly in a program.,A function will not execute automatically when a page loads.,A function will be executed by a call to the function.,Information can be passed to functions through arguments. An argument is just like a variable.

 {
    echo "Hello World!";
 }
75%

$result = file_get_contents('php://input');,I can not override the file_get_contents() function.,I can not override the file_get_contents() function. The work around is to monkey patch the class as a mock and override the constructor,@function file_get_contents($string) { return 'mock string'; }

$result = file_get_contents('php://input');

I can not override the file_get_contents() function. The work around is to
monkey patch the class as a mock and override the constructor (copying ALL
logic from the constructor to the mock allowing for $result to be taken
from the constructor's argument list).

file_get_contents()
load more v
40%

Example: php override built in functions

a.create new file with custom namespace,
   for example ovr.php
b.define all
function that neeed to be overrided
c.include ovr.php at the beginning of each file, add namespace to that file to use same namespace used by ovr.php

   //------------this is ovr.php
   <
   ? php declare(strict_types = 1);
namespace dk;
/**
 * override parse_url return empty string despite null
 * 
 * @param string $url
 * @param int $component
 * @return mixed
 */
function parse_url(string $url, int $component = -1): mixed {
   $ret = \parse_url($url, $component);
   if (is_null($ret)) $ret = '';
   return $ret;
}

//------------this is other-file.php
<
? php declare(strict_types = 1);
namespace dk;

$a = parse_url('/apath', PHP_URL_HOST);
var_dump($a);
22%

The function arguments, as a comma separated string. , The function to override. ,Example #1 override_function() example, The new code for the function.

load more v

Other "built-undefined" queries related to "Redefine Built in PHP Functions"