Welcome Guest, Not a member yet? Register   Sign In
Config Static Variables Not Working
#12

(This post was last modified: 08-04-2021, 07:20 AM by paulbalandan.)

Thanks. I think I know now.

1. You are not defining the $config in your BaseController. This means you are dynamically creating the property. Why is this a problem? First, when you call $this->config on BaseController you are basically creating it on the fly on that class. However, when you call $this->config on Admin the config is created anew because it is not there to begin with.

So you need to define the config explicitly.
PHP Code:
class BaseController
{
    
/**
     * Need to define this explicitly in the base class so that
     * extending classes have access to this. If not, each
     * child class would have their own versions of this.
     */
    
public $config;

    ...


2. Next, it seems you are only using static properties and methods in your config. Since these static members belong to the class, you can skip instantiating it using config(). Instead, you can directly invoke the statics from the class itself.
PHP Code:
# instead of calling like this
$this->config config('My_config');

# you can do this
# you do not need this again in admin, just in base is ok
$this->config = \Config\My_config::class;

# then initialize it
$this->config::load_values();

# now base and admin can access static props
echo $this->config::$user

proof that this works
https://3v4l.org/cXDLX

For #2 please note that the ::class notation should only be used as solution if strictly all properties and methods are static. If there's at least one that is not, you should use the config function
Reply


Messages In This Thread
Config Static Variables Not Working - by myo - 08-03-2021, 06:08 AM
RE: Config Static Variables Not Working - by myo - 08-03-2021, 08:08 PM
RE: Config Static Variables Not Working - by myo - 08-03-2021, 09:56 PM
RE: Config Static Variables Not Working - by myo - 08-03-2021, 10:37 PM
RE: Config Static Variables Not Working - by myo - 08-04-2021, 02:52 AM
RE: Config Static Variables Not Working - by paulbalandan - 08-04-2021, 07:18 AM
RE: Config Static Variables Not Working - by myo - 08-04-2021, 09:03 AM
RE: Config Static Variables Not Working - by myo - 08-05-2021, 03:57 AM
RE: Config Static Variables Not Working - by myo - 08-06-2021, 06:43 AM



Theme © iAndrew 2016 - Forum software by © MyBB