Are there advantages to using "use Function" for native PHP functions?

Asked

Viewed 178 times

12

When worked with namespaces, it is normal to see the use of use to import classes outside the local scope:

namespace Foo;

use Bar\Classe;
use DateTime;

It is also possible to do the same with functions:

namespace Foo;

use function Bar\funcao_qualquer;
use function is_null;

Checking out the documentation about name resolution it is possible to observe that when the name to be imported refers to a function and this does not exist in the namespace current, the name will be resolved to the global scope.

For unqualified Names, if no import Rule applies and the name refers to a Function or Constant and the code is Outside the global namespace, the name is resolved at Runtime. Assuming the code is in namespace A\B, here is how a call to Function foo() is resolved:

  1. It looks for a Function from the Current namespace: A\B\foo().
  2. It tries to find and call the global Function foo().

Since a native function will always be solved up to the global scope naturally, there is some advantage in importing it via use? For global constants the idea is the same?

I saw them do it in the implementation of Zend Diactoros where there is:

use function array_key_exists;
use function fclose;
use function feof;
use function fopen;
use function fread;
use function fseek;
use function fstat;
use function ftell;
use function fwrite;
use function get_resource_type;
use function is_int;
use function is_resource;
use function is_string;
use function restore_error_handler;
use function set_error_handler;
use function stream_get_contents;
use function stream_get_meta_data;
use function strstr;

use const E_WARNING;
use const SEEK_SET;
  • Do you know anything about this that’s not obvious or documented? It seems to me that it has no advantage because it is bringing to the current scope something that is already in the current scope. But it could be useful for some resolution mulambenta implementation problem :)

  • @Maniero The only reason I understood was to prevent these functions from being rewritten within the namespace, but I don’t know if that’s all.

1 answer

6


Is there an advantage in using the directive use in native functions and global within a namespace. There’s a reduction of processing time which, considering massive executions in load applications, make a difference. It makes no difference to our personal websites full of cobwebs.. But gain is gain!


Toon Verwerft, at the end of 2016, created a article (in English) which summarises a discussion of that time about the case and which was started on Twitter (in English). In the article, Verwerft also details the benchmark which made and reason for the difference: internally PHP checks first if there is a function in namespace and only then in the global by means of the opcode INIT_NS_FCALL_BY_NAME. There is processing cost.

According to Verwerft tests, the way to gain processing is to make fully qualified global function calls (QC) (Fully Qualified (FQ)). A fully qualified global function call eliminates the opcode INIT_NS_FCALL_BY_NAME. In the tests, there was a gain of 2.26% in relation to the call of global functions not completely qualified (NCQ) (non-fully Qualified (Non-fq)) - that are the normal calls.

There are two ways to make the global ones fully qualified:

<?php

// Método 1
namespace baz;
\foo();

// Método 2
namespace baz;
use function foo;
foo();

?>

At the end of the article, there is a list of community solutions made by Verwerft. From the list, I list some:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.