Re: Easy method of overriding built-in php functions.

From: Date: Wed, 16 Aug 2017 20:21:16 +0000
Subject: Re: Easy method of overriding built-in php functions.
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-100233@lists.php.net to get a copy of this message
On 16.08.2017 at 22:02, Ryan Jentzsch wrote: > I am aware of Advanced PHP debugger but what I need this type of > functionality for is mocking and testing. > > As an example I want to use PHPUnit mocking feature to create a mock of a > class. However in the constructor of this class is the following line to > get the expected JSON 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). > > This is awkward and in my use case the constructor also makes calls to > static methods in other objects which the latest version of PHPUnit does > not allow you to mock static methods. > > ADP is not acceptable to override functions in that it requires a binary to > be installed and php.ini to be configured as well as no support for this in > Windows. > > Is there an RFC that I didn't find that allows for easily overriding > built-in functions? For example: > > @function file_get_contents($string) > { > return 'mock string'; > } > > My C skills are quite rusty and understanding the Zend engine is a daunting > task or I'd create an RFC myself. The devil is in the details. The real > question I am asking is HOW DIFFICULT would something like this be to code > and include in the PHP core? There are already runkit and uopz which allow to override existing functions: * <https://pecl.php.net/package/runkit> * <https://pecl.php.net/package/uopz> -- Christoph M. Becker

« previous php.internals (#100233) next »