Skip to content

Instantly share code, notes, and snippets.

@acirtautas
Created February 8, 2013 07:09
Show Gist options
  • Save acirtautas/4737217 to your computer and use it in GitHub Desktop.
Save acirtautas/4737217 to your computer and use it in GitHub Desktop.
Example of PHP return on include, nice feature.
<?php
$data = include __DIR__.'/data.php';
var_dump($data);
<?php
$info = "Data string";
return $info;
@xeoncross
Copy link

Often used for configuration files

return array(
    'foo' => 'bar',
);

Then in your system you just make a simple wrapper

function config($file = 'default')
{
    static $config = null;

    if(empty($config[$file])) {
        $config[$file] = (object) require('config/' . $file . '.php');
    }

    return $config[$file];
}

Call it anywhere in your code

print config()->foo; // prints bar
print config('db')->username;

@acirtautas
Copy link
Author

Good point, I also find this feature very useful for configuration purposes, instead of doing file include and expecting some variable to be defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment