Yur Gasparyan
Yur Gasparyan

Reputation: 704

Composer PSR-4 for php file

In my project I have a folder lib\custom which contains Web folder and functions.php file. In my functions.php I have some functions which I need to use in another classes and in this file on the first line I have a defined namespace look like this

<?php
namespace Custom;

function abc(){....}

And in Web folder I have some classes with namespace Custom\Web;

In my composer.json file I have defined namespace look like this

"Custom\\":"lib/custom/"

So , now I am using the abc() look like this

use Custom;
$abc = Custom\abc("abc")

but as a response I am getting

Call to undefined function Custom\abc()

How can I solve this problem?

Upvotes: 1

Views: 1221

Answers (1)

Jakub Zalas
Jakub Zalas

Reputation: 36191

PSR-4 describes a specification for autoloading classes from file paths. It doesn't cover loading functions from files.

Use the files autoloader to load a file with functions on each request automatically. This will make your function available as long as you included the autoloader:

{
    "autoload": {
        "files": ["lib/custom/functions.php"]
    }
}    

Since your functions are namespaced you'll need to import them with the use statement or use the fully qualified name.

If your Web folder contains PSR-4 compatible classes, load them as before with the PSR-4 autoloader (you can define multiple autoloaders in your composer.json).

Upvotes: 3

Related Questions