I have a PHP related question.

I noticed a few frameworks make use of this structure of code and it makes absolutely no sense to me. There would be a PHP file, and the contents of the file would be as follows:


return [
‘a’ => ‘b’

My only understanding is that this is an array/object but I have no idea how these values would even be accessed.

Like, there is no array variable, how can a php file simply return something into a void? There isn’t a function or variable to catch that array.

How does this even work?

  • 7
    That’s really strange lol. In all my years of PHP I’ve never really seen that. Do you have an example with more context maybe?
  • 3
    @dfox I believe I saw this in Laravel. Lemme look up a reference haha.

    I saw this some time ago actually, but ever now and then the thought and confusion comes back to me. So I decided to finally ask today. 😂
  • 4
  • 9
    $your_stuff = include("that_file.php");
  • 5
    The only thing I could think of is that file being included inside a function or autoloader.

    Something like this:

    function godHelpUs(){

    return array();

    Now, I have a fealing this would actually work(never tried though), I've seen things 🥲
    But why someone would do it, that's a scary thought if it's own.
  • 2
    @C0D4 You're Close.


    And now look here:


    The "trick" is to bind a variable to an included file - you have a variable holding a lambda represented by the source code of the included file.

    (Not sure if my terminology is right, but I think the idea should be clear)
  • 3
    The first time I saw this was in laravel as well: I used this recently in a super small project for translations (instead of dealing with yaml-files). You can treat the file path like any other configuration detail so the "we just gonna load an array from a random file here lol"-part is a bit less shocking to other readers of your code.
  • 2
    @IntrusionCM looking at the repo, it's their way of auto loading the config.

    Makes sense, but yea needs context for the usage.

    @TjasPRG has it.

    Still, an awkward way of doing it, I'd just write a config class and be on my way with it being autoloaded.
  • 1
    @C0D4 yes.

    That's btw the reason they use require instead of include.

    Require goes boom with an error, include continues....

    The pattern is not uncommon...

    Symfony and other frameworks do it, too.

    But you have to dig deep to find that shit.
  • 4
    @IntrusionCM @C0D4 and to the OP.

    In a project made with a couple of libraries, like the fast-route router library (personal favorite of mine for small stuff) I have a section in which I can include all my routes, the way in which I use a Routes.php file where I put my routes it to return an array of routes with the http method, the url route configuration and then the namespace representation of the controller structure.

    Then on my bootstraper I do something along the lines of:

    $dispatcher = \FastRoute\simpleDispatcher(
    function (\FastRoute\RouteCollector $r) {
    $routes = include(ROOT_DIR . '/location of routes.php');
    foreach($routes as $route) {

    And voila, I can obtain the route info from it, l believe this is similar to what @IntrusionCM said.

    Pretty common in some of my projects to do small shitty configurations like these when I don't want to go full yaml/json/tumama
  • 2
  • 1
    @AleCx04 see my comment on include vs require.

    Use require if you don't want missing files make your life a cookie party of sadness.
  • 0
    @IntrusionCM ah yes I did see it. You are right my good sir.

    I'll change that before I get any accidents. Was my code similar to what you mentioned earlier though? cuz I want to keep adding to my bag of tricks
  • 1
    @AleCx04 yes.

    as PHP is a scripting language, require and include execute the scripts they refer to.

    It's one of the reasons auto loading and PSRs became so important - to disable side effects.

    Eg. Class definitions shouldn't contain additional commands
  • 1
    With auto loading I mean the nutjob that some people did prior to 5.3 I think....

    The "one" include.

    One in the sense of an extreme pile of shit file containing everything.

    From bootstrapping to classes to further includes to all kind of bullshit bonanza.

    The one include files were klingon pain sticks up in your arse...

    Autoloading and the PSRs regarding to code standards / autoloading and so on made PHP a way better place.

    I still remember PHP 4s Zend registry framework.

    And by remember I mean... The CIA torture department would get a boner if they knew how much this shit could fuck u up.
  • 1
    This does seem like a messy way of loading configuration file, similar to @C0D4 I too would prefer having a configuration class but bonded with a .env file.

    Not only does Laravel’s approach look messy to me, it lacks context, and leads to a lot of guess work, which makes the framework seem more complicated.

    But thank you all so much for clearing this out. I had absolutely no idea this was a method that could potentially be used. The first time I saw this about 4 years ago, I really thought this was some advance way of writing code and I just wasn’t there yet. But now that I think of it, it isn’t that complicated but definitely not a method I would want to use.

    By the way, what are the benefits to using this method over defined Constants? I assume both need to remain unchanged...
  • 2
    @uziiuzair https://php.net/manual/en/...

    constant requires to have scalar values.

    The approach is the compromise of the need to have non-scalar -/ even dynamic code execution and a centralized access to it.
  • 1
    @IntrusionCM which you can get around with having a .env / env.php and load that with the php.ini

    Constants can be dynamic since since something else is defining them.

    But.... that approach takes code outside of the project. Another approach but similar problem, you either deal with it inside the project (like the frameworks) or go a level up and deal with it there.

    Each to their own 🤷‍♂️

    Personally I prefer a dynamic env then a dynamic config class.
  • 0
    Just php things... there is good reasonnto change language
  • 0
  • 0
    @KDSBest @AleCx04

    The fun thing here is... This is true for any scripting language.

    Keep the shit for yourself. :)

    E.g. Python's "equivalent" can be seen in the __init__.py .

    It was specifically made to handle module related initialization...

    Bash is the same: eg. source (.) includes and executes, too.

    Has nothing to do with PHP specifically, you will find in most scripting languages equivalents or stuff that does nearly the same.

    Which is... Only logical.

    Without an include -/ require system, the scripting language would be non extensible.

    That would be bad TM.
  • 0
    @IntrusionCM just because all those languages you mentioned allow that. I need to find it great?

    I don't like any of your mentioned language. Still they have reason to exist and you do you.

    I don't do them. No need to take it personal, just you don't agree with me.
  • 0
    @KDSBest I didn't take it personal...

    I just dislike the general approach of saying 'change the language'.

    And your comment was wrong.

    So I said "keep the shit for yourself" and an smiley after it

    I'm not mad. Quite the opposite. Rather amused... For me your comments suggests a lack of experience
Add Comment