Re: Autoloading functions/consts without a performance impact

From: Date: Fri, 03 Jan 2020 14:57:16 +0000
Subject: Re: Autoloading functions/consts without a performance impact
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-107975@lists.php.net to get a copy of this message
On Fri, 3 Jan 2020 at 14:39, tyson andre <tysonandre775@hotmail.com> wrote: > > namespace Foo; > > echo strlen('hello'); // Finds a global function at step 2, so doesn't > > trigger the autoloader > > echo \Foo\strlen('hello'); // Explicitly namespaced function, so triggers > > the autoloader > > echo strlen('hello'); // Should this run \strlen or Foo\strlen? > > The example you were thinking of would call \strlen on line 2, \Foo\strlen > on line 3, and \Foo\strlen on line 4. > (because the caching is per-opcode, not per-file or per-namespace) > Sorry, I should have been clearer - I wasn't asking for a literal answer based on any particular implementation, I was asking rhetorically what a user would expect to happen. Let's make the example a tiny bit more complex: namespace Foo; function one() { echo strlen('hello'); } function two() { echo Foo\strlen('hello again'); } Now the behaviour of my program can completely change depending on which of those functions I call first, which might even depend on user input. Regardless of exactly how the cache works, that kind of unpredictability is a recipe for disaster. The only way to make it predictable again is to pre-define the namespaced function in my startup code, at which point I don't need function autoloading. Regards, -- Rowan Tommins [IMSoP]

« previous php.internals (#107975) next »