How to use namespaces without classes with autoload from Composer?

Asked

Viewed 470 times

2

I’m trying to use the classless namespaces with the Poser autoload, but I believe it is failing, for example:

/Foo/Bar.php

namespace Foo\Bar {
    function baz() {
        echo 'Funcionou';
    }
}

/index php.

require './vendor/autoload.php';

Foo\Bar\baz();

Running the index file generates the following error:

Fatal error: Uncaught Error: Call to Undefined Function Foo Bar Baz() in /[...]/basic/index.php on line 5

but if I previously include the file:

/index php.

require './Foo/Bar.php';

Foo\Bar\baz();

So it works. I believe when calling Foo\Bar\baz he’s trying to include the file Foo\Bar\baz.php, but I’m not sure

My autoload on composer.json:

"autoload": {
    "psr-4": {
        "Foo\\": "./Foo"
    }
}

I did the dump-autoload

Use is in a framework, where the user can create routes by passing a function with its namespace to be executed (for example, Router::get('/foo', 'App\Foo\get');), as there is usually no need to save status, the class becomes unnecessary. Use of property files Poser solves the problem, but does not allow Lazy load (load only what is used, that is, on demand), which can mean many files included for nothing

  • Cade the file Composer?

  • @Virgilionovic the composer.json? It’s practically blank like I said

  • So, it won’t work if it’s blank so it doesn’t carry anything!

  • @Virgilionovic altered the file as I imagine it should and I guessed at the question

2 answers

1

I still don’t understand why you use it this way, you just want to call the correct function? If so it would be easier to create a helper and through it you create the function and call the same.

For this, I added the following line in my Poser indicating the location of my helper.

"autoload": {
    "files": [
        "app/Helpers/helper.php"
    ],
    "psr-4": {
        "App\\": "app/"
    }
}
  • I edited the question, I hope it’s clear

1


This type of implementation is corresponds to the folder organization, the namespace should have the same path as your folder, which is not what happens in your question, the correct would be to create the folder directory as follows:

/Foo/Bar/Bar.php

and its namespace correspondent

Foo/Bar

in the configuration file composer.json configure as follows:

{
    "name": "Virgilio/Pack",    
    "autoload": {
        "psr-4": {
            "Foo\\Bar\\": "Foo/Bar"
        }   
    }
}

that is, the first key is the namespace and its assignment the folder path (or folders), now run the command php composer.phar dump.

Another is that this implementation is for load of classes rather than functions, the function is in the key files which should be implemented with the file path, example:

{
    "name": "Virgilio/Pack",    
    "autoload": {
        "psr-4": {
            "Foo\\Bar\\": "Foo/Bar"
        },
        "files": [
            "Foo/Bar/Bar.php" 
        ]
    }
}

and turn the command php composer.phar dump.

Remember that in this file Bar.php it is only right to have functions or classes and also if you are going to have only functions you do not need to put namespace, but, nothing prevents, I believe hinder typing at the time of programming.


Summary would set up like this:

{
    "name": "Virgilio/Pack",    
    "autoload": {           
        "files": {
            "Foo/Bar/Bar.php" 
        }   
    }
}

and would use as the namespace thus:

echo \Foo\Bar\baz();

or withdraw that namespace which I think is unnecessary in the archives Bar.php and digital:

echo baz();

and try to make code cleaner and less complicated for use.


Another basic example: namespace com php and a recommended reading The Composer.json Schema - PSR-4#

  • But adding to the list of files will all be loaded together at the beginning, and not as used (if used), right?

  • I edited the question, I hope it’s clear, I’d appreciate it if you could help

  • @Guilhermecostamilam you said but does not allow Lazy load (load only what is used, ie on demand), which can mean many files included for nothing, with functions has to be done by files this is how the configuration file demonstrates in its documentation. If class has the lazy load effect, although classe usually we put one in each file to work, already function are several in one file (can be one too, but, usually we put several), because you do not change the strategy then.?

  • continuing ... @Guilhermecostamilam working with classes, since it is a framework, well what the framework is?

  • https://costamilam.github.io/Alpha. If I created a proper function for autoload, it might work?

  • is a micro framework, look only if you can use what I did without problem, will have to be so even with files configured @Guilhermecostamilam

  • You could then just add a note in your reply that Composer does not support Lazy load without classes?

  • It would be a mistake for me to say that, because, composer is the package manager and PHP that is responsible for loading the features. @Guilhermecostamilam If I put that my answer is wrong, your original question has the explanation of how is the best way to up the functions.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.