![]() |
Config Static Variables Not Working - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Config Static Variables Not Working (/showthread.php?tid=79839) Pages:
1
2
|
Config Static Variables Not Working - myo - 08-03-2021 Hi, I am using a custom Config file. I populate its static variables by calling a method & then use those variables in BaseController. Its working fine in BaseController but when I try to use those same variables in AdminController extended from BaseController its variables show empty. I don't call the method to populate its variables in AdminController as its extended from BaseController. Following are details. Kindly help me understand why config file variables don't show any value in AdminController: BaseController PHP Code: $this->config = config('My_config'); AdminController extends BaseController PHP Code: $this->config = config('My_config'); $this->config::$users_array[0]; prints values in BaseController but don't print anything in AdminController. Please advise. Regards RE: Config Static Variables Not Working - paliz - 08-03-2021 look this my config PHP Code: <?php namespace Modules\Common\Config; RE: Config Static Variables Not Working - ikesela - 08-03-2021 config is mean for variable and value (for stand alone purpose) , others better use models or library RE: Config Static Variables Not Working - paulbalandan - 08-03-2021 Do not call `config` again in AdminController RE: Config Static Variables Not Working - myo - 08-03-2021 (08-03-2021, 07:20 PM)paulbalandan Wrote: Do not call `config` again in AdminController Then how to access config in Admin Controller? I tried calling PHP Code: parent::$users_array[0]; PHP Code: $this->$users_array[0]; As config is not an object, its just loaded and used & the variables are static so how I should access them in Admin Controller? RE: Config Static Variables Not Working - paulbalandan - 08-03-2021 What is the visibility of $this->config in BaseCOntroller? If set to private, AdminController can't access it. Needs to be at least protected or public. RE: Config Static Variables Not Working - myo - 08-03-2021 (08-03-2021, 09:36 PM)paulbalandan Wrote: What is the visibility of $this->config in BaseCOntroller? If set to private, AdminController can't access it. Needs to be at least protected or public. I am just accessing it using PHP Code: $this->config = config('My_config'); PHP Code: $this->config::load_values(); As per documentation // Get shared instance with config function $config = config('Pager'); RE: Config Static Variables Not Working - paulbalandan - 08-03-2021 Ok. I meant how you define it in your class? PHP Code: class BaseController RE: Config Static Variables Not Working - myo - 08-03-2021 (08-03-2021, 10:15 PM)paulbalandan Wrote: Ok. I meant how you define it in your class? I dont declare any variable like this. I think the real question is that I access a shared instance using $this->config = config('My_config'); and then populate its static variables like following: $this->config::load_values(); Now as the variables in 'My_config' are static, they must maintain the values throughout the life of the process so when I access the same shared instance using $this->config = config('My_config'); in another controller, it should have the static values intact. But actually they have empty values. Why? Either $this->config = config('My_config'); creates a new instance & dont provide the shared instance or there is a way to access the static variables that I am unaware of. RE: Config Static Variables Not Working - paulbalandan - 08-04-2021 I think I have a guess on the problem but needs some confirmation. Can you share the code of your My_config? Class objects are saved by reference by default so changing one should affect the other. I just need to confirm the structure of your config class to know the culprit behind this. |