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

(This post was last modified: 08-04-2021, 09:34 AM by myo.)

(08-04-2021, 07:18 AM)paulbalandan Wrote: 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

No, it did not resolve my issue. I have mixed methods in config file so can't use ::class. 
Following is simplified version of the problem. You can check it here: https://3v4l.org/9G2Ab and also try it in Codeigniter. 
I think  "function initController()" in "BaseController" is not executed like " function __construct()" automatically when its extended by another class as my all code in BaseController is inside "function initController()". Am I right? 
If you can make following code print "100" in AdminController in Codeigniter then my issue is resolved.

PHP Code:
<?php
 
 
class BaseController {
 
 public static 
$var_test '#';
 
   public function __construct() {
     self::$var_test "100";
   }
 }
 
 
 class 
AdminController extends BaseController {
 
    public function render() {
 
         echo parent::$var_test// Prints 100 here (www.3v4l.org/9G2Ab) which is correct
                                 // Prints # in CI 4 AdminController extended from BaseController which is wrong 
   }
 }
 
 
$adm = new AdminController;
 
var_dump($adm->render());
?>
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 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