How can I make an array accessible into a class without passing it?

Viewed 843

I have a .ini file as configuration of my project. Now I want to make it accessible everywhere. Here is a simplified of 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 see in the fiddle, it throws:

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

Noted that when I use global keyword for it, it throws syntax error.


Look, I can pass the array to the class like this:

$obj = new myclass($config);

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

But I cannot do that every time for all classes. Anyway, I want to know, is it possible to make an array accessible in the global scope?

6 Answers
Related