CodeIgniter Forums
Why using the "use function" import statements for PHP native functions? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: General (https://forum.codeigniter.com/forumdisplay.php?fid=1)
+--- Forum: Lounge (https://forum.codeigniter.com/forumdisplay.php?fid=3)
+--- Thread: Why using the "use function" import statements for PHP native functions? (/showthread.php?tid=89131)



Why using the "use function" import statements for PHP native functions? - Muzikant - 01-13-2024

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.


RE: Why using the "use function" import statements for PHP native functions? - kenjis - 01-13-2024

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.


RE: Why using the "use function" import statements for PHP native functions? - Muzikant - 01-16-2024

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.