2017-09-15 20:52 GMT+03:00 Ryan Pallas <derokor...@gmail.com>:

> On Fri, Sep 15, 2017 at 11:38 AM, <ilija.tov...@me.com> wrote:
>
> > Hi Ryan
> >
> > I can see your argument. The reasoning behind it is that a function in
> the
> > standard library should not encourage unsafe code. Admittedly, since this
> > function is rarely used except for templating systems one could call
> this a
> > non-issue. I just wanted to bring it up.
> >
>
> That makes sense. What about performance difference? On a reasonable sized
> array (not 10k items and not 2) which is faster?
>
> extract($array);
>
> - or -
>
> foreach ($array as $key => $value)
>     $$key = $value;
>
> Honestly, I don't know (ps 2 lines without braces instead of 3!)
>
>
> >
> > Regards
> >
> >
> > On 15 Sep 2017, 19:30 +0200, Ryan Pallas <derokor...@gmail.com>, wrote:
> >
> >
> >
> > On Sep 15, 2017 11:22 AM, <ilija.tov...@me.com> wrote:
> >
> > Hi!
> >
> > The `extract` function takes an associative array and puts it into the
> > local symbol table.
> > http://php.net/manual/en/function.extract.php
> >
> > ```
> > $array = [
> >     ‘foo’ => ‘foo’,
> >     ‘bar’ => ‘bar’,
> > ];
> >
> > extract($array);
> >
> > print $foo; // "foo"
> > ```
> >
> > As a second parameter the `extract` function takes some options to make
> > this function less dangerous, like `EXTR_SKIP` that prevents an existing
> > local variable of being overwritten. There’s a few more options, go ahead
> > and take a look at the documentation. `EXTR_OVERWRITE` is the default one
> > though. You can also pass a prefix for the variable names as a third
> > argument.
> >
> > I seriously doubt the usefulness of this function, especially looking at
> > the potential risks. The fact that overwriting the local variables is the
> > default behaviour doesn’t make it any better. I suggest deprecating it in
> > PHP 7.3 and removing it in 8.
> >
> > In a whole Symfony-Stack (3.4) with all of it’s dependencies I could only
> > find two usages of this function, both of which could be easily rewritten
> > in vanilla PHP:
> > https://github.com/symfony/symfony/blob/master/src/Symfony/
> > Component/Templating/PhpEngine.php#L148
> > https://github.com/symfony/symfony/blob/master/src/Symfony/
> > Component/Templating/PhpEngine.php#L158
> >
> > Only downside: A polyfill is probably impossible since you cannot mutate
> > the local symbol table of the callee (as far as I’m aware).
> >
> > Any thoughts?
> >
> >
> > I see no gain by removing this function. I've also seen it used for
> > templating quite often. Yes the functionality could be changed not to use
> > extract and end up working the same to the consumer but why make people
> > rewrite these things for no apparent gain (and likely a small performance
> > hit)?
> >
> >
> > Regards
> >
> >
> >
> >
>

Hi Ryan,
well, basically, none. Results are from a Q6600 machine and under windows,
so your mileage probably gonna be quite better :)

C:\Users\psihius\Documents\web>php -v
PHP 7.1.5 (cli) (built: May  9 2017 19:48:36) ( NTS MSVC14 (Visual C++
2015) x64 )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
C:\Users\psihius\Documents\web>php -d memory_limit=-1 test.php
6.884626150131226 - Creating arrays
2.035606861114502 - foreach
2.128609180450439 - extract

The code:

define('ITERATIONS', 10000000);

$__time = microtime(true);

$__array = $__array2 = [];
for ($__i = 0; $__i < ITERATIONS; ++$__i) {
    $__array['a'.$__i] = $__i;
    $__array2['b'.$__i] = $__i;
}
echo number_format(microtime(true) - $__time, 15, '.', ''), PHP_EOL;

$__time = microtime(true);
foreach ($__array as $__key => $__variable) {
    $$__key = $__variable;
}
echo number_format(microtime(true) - $__time, 15, '.', ''), PHP_EOL;

$__time = microtime(true);
foreach ($__array2 as $__key => $__variable) {
    $$__key = $__variable;
}
echo number_format(microtime(true) - $__time, 15, '.', ''), PHP_EOL;

-- 
Arvīds Godjuks

+371 26 851 664
arvids.godj...@gmail.com
Skype: psihius
Telegram: @psihius https://t.me/psihius

Reply via email to