Re: [PHP-DEV] [RFC][DISCUSSION] Deprecation of fallback to root scope

  101748
February 3, 2018 10:11 netmo.php@gmail.com (Wes)
I get that people want to do that, it's so comfortable. But is it really a
good idea? When I have that kind of problem, I use:

class Foo{
    protected $date = "date";
    function getDate(){
         return ($this->date)();
    }
}

$mockFoo = new class extends Foo{ protected $date = "my_fake_date"; };

Or dependency injection (I inject the function as closure in the ctor).
Alternatively, you could consider using https://github.com/krakjoe/uopz
which allows to redefine functions at runtime.
  101751
February 3, 2018 12:15 netmo.php@gmail.com (Wes)
question was:
> Do I get that right that it will then no longer be possible to mock functions from the root namespace during tests?
> At least when the functions are "use"d or are explicitly namespaced... > Being able to mock functions like "date" or "time" or "mt_rand" is currently extremely helpful.
  101752
February 3, 2018 12:18 ocramius@gmail.com (Marco Pivetta)
On Sat, Feb 3, 2018 at 1:15 PM, Wes php@gmail.com> wrote:

> question was: > > Do I get that right that it will then no longer be possible to mock > functions from the root namespace during tests? > > At least when the functions are "use"d or are explicitly namespaced... > > Being able to mock functions like "date" or "time" or "mt_rand" is > currently extremely helpful. >
I think there's something quite broken with the current approach in mocking core functions, specifically because already having them hardcoded in your codebase means that the code relies on hidden shared global state. Can strongly suggest injecting a `Clock` object as is done in https://github.com/lcobucci/clock, for example. Same goes for sources of randomness. Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/