A
A
Alexey Nikolaev2016-01-24 22:31:37
PHP
Alexey Nikolaev, 2016-01-24 22:31:37

What is the best practice for loading third party files in a function?

Good evening.
What is the best way to load a php file in a function? Some config is assumed, for example:

$config = [
   'test' => 'test'
];

return $config;

The simplest option is include, but it's ugly. Which way is 100% correct?..
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dmitriy, 2016-01-24
@dmitriylanets

function test(){
   $container = &container_instance();//singliton
   $config = $container->get('config');
}

namespace Acme\ServiceProvider;
use League\Container\ServiceProvider\AbstractServiceProvider;
class ConfigProvider extends AbstractServiceProvider
{
protected $provides = [
        'config'
    ];
public function register()
{
     $this->getContainer()->add('config', function(){
         return [
             'test' => 'test'
         ];
     });
}
}

G
Grigory Vasilkov, 2016-01-27
@gzhegow

and I just did it differently:

$arr = json_decode(file_get_contents());
$app = Framework::init();
$app->config = array_merge($app->config, $arr);

and so on at least 500 configs to combine into one.
True, there is a jamb there, you will have to rewrite array_merge to work with numeric keys
. I can throw off the code if you like the method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question