Welcome Guest, Not a member yet? Register   Sign In
Why using the "use function" import statements for PHP native functions?
#1
Question 
(This post was last modified: 01-13-2024, 08:50 AM by Muzikant.)

Hi. I am strugling to understand, why to use "use function" import statement for PHP native functions. An example code:

PHP Code:
<?php

namespace Acme\Package;

use 
Acme\Package\SomePackage;

// why this?
use function md5;
use function 
json_encode;

class 
AnotherPackage {
    // some code with using of a md5 and json_encode functions


Can you write me, why to import the PHP native functions? I have no idea and could not find an answer.
Reply
#2

1. All PHP native functions are defined in global namespace. That is like \md5(), \json_encode().
2. We can define the same name function in another namespace like \Acme\Package\md5().

If you write `use function md5;`, you will surely call \md5(), when you write code `md5('foo')`.
If you don't write `use function md5;`, \Acme\Package\md5() will be called if defined.
Reply
#3

I understand it now, thank you. In the case I found it, it is an another security level to avoid discrepancies while working with third party code I guess.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB