Doc #79675 [Com]: Incorrect docs for array_key_exists opcode
| From: | paul at namepros dot com | Date: | Thu, 04 Jun 2020 17:10:09 +0000 |
| Subject: | Doc #79675 [Com]: Incorrect docs for array_key_exists opcode | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-17635@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79675&edit=1
ID: 79675
Comment by: paul at namepros dot com
Reported by: paul at namepros dot com
Summary: Incorrect docs for array_key_exists opcode
Status: Not a bug
Type: Documentation Problem
Package: Documentation problem
Operating System: Linux 5.4.0
PHP Version: 7.4.6
Block user comment: N
Private report: N
New Comment:
Ah, thanks; I knew I must be missing something. I should've double-checked https://www.php.net/manual/en/language.namespaces.importing.php.
Previous Comments:
------------------------------------------------------------------------
[2020-06-04 15:59:09] nikic@php.net
You are importing the array_key_exists class, not the array_key_exists function. You're looking
for "use function array_key_exists".
------------------------------------------------------------------------
[2020-06-04 15:33:48] paul at namepros dot com
I attempted to fix the documentation myself, but https://edit.php.net/?project=PHP&perm=en/migration74.other-changes.php
times out.
------------------------------------------------------------------------
[2020-06-04 15:23:58] paul at namepros dot com
Description:
------------
---
From manual page: https://php.net/migration74.other-changes
---
On the aforementioned manual page, I read the following:
> A specialized VM opcode for the array_key_exists() function has been added, which improves
> performance of this function if it can be statically resolved. In namespaced code, this may require
> writing \array_key_exists() or explicitly importing the function.
This confirmed my assumption that
use array_key_exists; should be a valid way to ensure
that this new optimization is utilized in namespaced code. However, I was surprised to find that it
doesn't seem to work according to VLD. Here's namespace-less code:
% php -dvld.active=1 -dvld.execute=0 -r 'array_key_exists(1, []);'
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename: Command line code
function name: (null)
number of ops: 3
compiled vars: none
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
1 0 E > ARRAY_KEY_EXISTS ~0 1, <array>
1 FREE ~0
2 > RETURN null
branch: # 0; line: 1- 1; sop: 0; eop: 2; out0: -2
path #1: 0,
But here's what I get with a namespaced version with use array_key_exists;:
% php -dvld.active=1 -dvld.execute=0 -r 'namespace a; use array_key_exists; array_key_exists(1,
[]);'
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename: Command line code
function name: (null)
number of ops: 5
compiled vars: none
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
1 0 E > INIT_NS_FCALL_BY_NAME
'a%5Carray_key_exists'
1 SEND_VAL_EX 1
2 SEND_VAL_EX <array>
3 DO_FCALL 0
4 > RETURN null
branch: # 0; line: 1- 1; sop: 0; eop: 4; out0: -2
path #1: 0,
Not what I expected, but https://www.php.net/manual/en/language.namespaces.faq.php#language.namespaces.faq.shortname2
seems to confirm that this is the expected behavior. The only way to optimize function calls like
array_key_exists and is_array in namespaced code appears to be to use the
fully qualified name, e.g. \array_key_exists.
Test script:
---------------
<?php
namespace a;
use array_key_exists;
array_key_exists(1, []);
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79675&edit=1