On 2/8/16 11:46 AM, Kinn Julião wrote:
On Mon, Feb 8, 2016 at 11:38 AM, Rowan Collins <rowan.coll...@gmail.com>
wrote:

Kinn Julião wrote on 08/02/2016 16:05:

That's the nice thing about userland... you are giving them ability to
implement what they want!
`array_filter([...], 'is_int', ARRAY_FILTER_USE_KEY)` does the job as the
others... don't think having a new function would prevent this.

It does the job, but it is much less efficient in some cases than the
foreach and return false case, as it must examine every element; it also
has unnecessary memory overhead, creating a filtered list which exists only
to be counted and thrown away; and an internal implementation could be
optimised even further by exploiting the way the engine stores the array
internally.

How does `array_filter([...], 'is_int', ARRAY_FILTER_USE_KEY)` add that
*much* overhead?
And if that's the case, instead of introducing a 99999 array function into
the core, why not just create a new Object then?

$list = list('foo','bar'); // I know it's a reserved token, don't have time
to find a better synonym. it's just a sample
$list[2] = 'baz';

var_dump($list);

string(1) "foo"
string(1) "bar"
string(1) "baz"

$list['a'] = 'foobar'; // Fatal error.

I'm confused at the goal here. Is the intent "I need to do different logic if this is a numeric array vs. something else" or "I need to make sure this is a numeric array, because things break otherwise"?

If the latter, $foo = array_values($bar); is a reasonably efficient and reasonably bulletproof way to ensure you have a strictly numeric array.

PHP is at this stage expressive enough that a large number of core
functions could be rewritten in multiple ways using other core functions,
but that doesn't automatically rule out adding more functions to core where
it is useful to do so.

PHP is at a stage where we can add support to a better structure and
objects support... we don't need a function to check if the keys are
integer, we need an object that only supports integer as keys, or even a
generic key...

Would rather have `Map<int, mixed>` than `has_numeric_keys`

Or that. Which I like even better. Ahead-of-time typing is much safer at scale than runtime typing. (Of course we then run smack into the arrays-and-objects-are-just-different-enough-to-be-annoying problem that has plagued us for a decade.)

--
--Larry Garfield


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to