use function doesn't import functions in PHP
Asked Answered
M

1

9

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.


enter image description here

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.

Mandy answered 26/7, 2018 at 21:8 Comment(15)
You need to look at "auto loaders". See the SPL and also maybe take a peek at: php-fig.org/psr/psr-4Arching
@Arching is this auto loader related though? use function as described here php.net/manual/en/language.namespaces.importing.php specifically uses the use function syntaxMandy
You're telling composer that the My namespace corresponds to your project's root. Any class in the root dir should therefore be in the My namespace; not in the My\Full namespace. If Full.php (capital F) would define a class Full, it could be autoloaded. For functions I guess it's the same deal.Boil
@Boil php.net/manual/en/language.namespaces.importing.php specifically uses the use function My\Full\functionName as func; to import functions, which was available since PHP5.6 unless I'm interpreting that incorrectly.Mandy
Use of word importing is confusing in context of PHP namespaces. Would say yes, it is auto loader related.Arching
@Mandy Yes I just read it after I had submitted my comment. Updated it. Thanks for pointing it out!Boil
@Arching In that case let me edit the question, to remove autoloading since that's irrelevant. It was more for the general setup, in case anyone could reproduce my issue.Mandy
Sorry, I'm rereading question also. Didn't mean to jump gun. Reproduce with a require statement in there then if not auto load related?Arching
@Arching that's fine, happens. Specifically trying to avoid include or require for learning purposes. I don't believe use function would be included in PHP if not for the purpose of importing functions. Unless I have misread the documentationMandy
this comment on the PHP man page is accurate: "The <?php use ?> statement does not load the class file. You have to do this with the <?php require ?> statement or by using an autoload function."Arching
@Arching But even with the use of the autoloader, it doesn't work. Which is the main issue. The other related questions don't answer how to use use function keywords specifically.Mandy
@Boil not an issue with the namespace, when I replace the function with a class in full.php fileMandy
I don't use composer... maybe look at last section on this page? getcomposer.org/doc/01-basic-usage.mdArching
@Arching thanks..but I don't think this is related to composer setup/configuration specifically, as using regular classnames work. Just the specific syntax doesn'tMandy
I upvoted so hopefully you get more views. Honestly confused by it not being an auto loader question but you can't reproduce with out using this auto loader. Then, I'm also behind on sleep.Arching
B
10

use function does not include any files or function definitions it simply aliases a fully qualified function name meaning when you call the function you don't need to specify the namespace.

In your example you are using composer which is great for automatically including files however from https://www.php-fig.org/psr/psr-4/ PSR-4 is

a specification for autoloading classes from file paths

It does not autoload functions or files which don't conform to this specification.

You can however use composer to automatically include files for situations like this. You need to update your composer.json then run composer dumpautoload

composer.json

{
    "autoload": {
        "files": ["full.php"]
    }
}

The rest of your can then remain unchanged and it should work.

Beare answered 26/7, 2018 at 22:30 Comment(4)
Then what is the purpose of use function, and in what context would it be used?Mandy
Use function just means you don't need to use the fully qualified namespace when calling the function. You are giving it an alias. See php.net/manual/en/language.namespaces.importing.phpBeare
how would you use it, could you give me a code example?Mandy
You would use it exactly as you have but you either need to include or require the file containing the function definition manually or if you are using composer, as it seems you are, you can register files to be automatically included when including vendor/autoload.php. I am on my mobile at the moment. I can update my answer with more details tomorrowBeare

© 2022 - 2024 — McMap. All rights reserved.