How can I make an array available to a class without passing it?

I have a file .inias my project configuration. Now I want to make it available everywhere. This simplifies what I'm trying to do:

<?php

$config = [];
$config['base_url'] = '/mypath/';

class myclass{

    public function myfunc(){
        print_r($config);
    }
}

$obj = new myclass;
$obj->myfunc;

As you can see on the violin, he throws:

Note: Undefined property: myclass :: $ myfunc in / in / iNZOv on line 14

It is noted that when I use a keyword globalfor it, it throws a syntax error.


Look, I can pass the array to the class as follows:

$obj = new myclass($config);

public $config;
public function __construct($config)
{
    $this->config = $config;
}

But I can not do this every time for all classes. In any case, I want to know if it is possible to make the array accessible in the global scope?

+4
6

global. , . .

class Config
{
    public static $values = [
        'base_url' => '/mypath/'
    ];

    /**
     * @param string $key
     * @return null|string|int|mixed
     */
    public static function get($key)
    {
        return empty(self::$values[$key]) ? null : self::$values[$key]; // alternatively you can throw an exception for a missing key.
    }
}

class Foo
{
    public function printConfig()
    {
        print_r(Config::$values);
    }
}

(new Foo)->printConfig();
echo Config::get('base_url');
0

global. $obj->myfunc(); $obj->myfunc();, -

$config = [];
$config['base_url'] = '/mypath/';

class myclass{

    public function myfunc(){
        global $config;
        print_r($config);
    }
}

$obj = new myclass;
$obj->myfunc();
+1

myfunc - , . $obj->myfunc; $obj->myfunc();

, global config .

$config = [];
$config['base_url'] = '/mypath/';

class myclass{

    public function myfunc(){
        global $config;
        print_r($config);
    }
}

$obj = new myclass;
$obj->myfunc();

, Globals .

+1

, .

" " $_GLOBAL["variable_name"];

, .

+1

. . factory . , $obj = new MyClass(); , , factory MyClass, .

, . , . MyClass , . .

Pimple Aura DI, .

Here is an example for Pimple (if your project uses a composer). Go to the root of the project and get a pimple:

 $ composer require pimple/pimple ~3.0

Create and configure the container in the early stages of your application:

use Pimple\Container;

$dic = new Container();
$dic['config'] = function ($c) {
    return ['base_url' => '/mypath/'];
};

$dic['myclass'] = function ($c) {
    return new MyClass($c['config']);
};

Now you can get your class anywhere in the application by simply typing:

$obj = $dic['myclass']; // $obj is a new, shiny MyClass instance here

Your MyClass signature should look like this:

private $config; // Always start with reduced visibility

public function __construct(array $config)
{
    $this->config = $config;
}

public function myfunc() {
    print_r($this->config);
}
+1
source

A better solution is to use a custom function like this:

<?php

function config( $key = null ){

    // $config = parse_ini_file('../out_of_root/app.ini');
    $config = [];
    $config['base_url'] = '/mypath/';

    if ( is_null($key) ){
        return $config;
    } else if ( array_key_exists($key, $config) ) {
        return $config[$key];
    } else {
        return "key doesn't exist";
    }
}

class myclass{

    public function myfunc(){
        echo config('base_url');
    }
}

$obj = new myclass;
$obj->myfunc();

Demo

0
source

Source: https://habr.com/ru/post/1682027/


All Articles