Can't import functions using the use function
keywords described in PHP.net. Recreating the example locally is returning a PHP Fatal error: Uncaught Error: Call to undefined function
.
composer.json
{
"autoload": {
"psr-4": {
"My\\": ""
}
}
}
full.php
<?php
namespace My\Full;
function functionName()
{
echo 'Hello Stackoverflow';
}
index.php
<?php
require 'vendor/autoload.php';
use function My\Full\functionName as func;
func();
Note: I understand I can require the file, but I wanted to know if it was possible without doing so.
use function
syntax – MandyMy
namespace corresponds to your project's root. Any class in the root dir should therefore be in theMy
namespace; not in theMy\Full
namespace. If Full.php (capital F) would define a classFull
, it could be autoloaded. For functions I guess it's the same deal. – Boiluse function My\Full\functionName as func;
to import functions, which was available since PHP5.6 unless I'm interpreting that incorrectly. – Mandyimporting
is confusing in context of PHP namespaces. Would say yes, it is auto loader related. – Archingrequire
statement in there then if not auto load related? – Archinguse function
would be included in PHP if not for the purpose of importing functions. Unless I have misread the documentation – Mandyuse function
keywords specifically. – Mandy