PHP class private variable set from another (included) file

I have a class that uses private variables, these variables are "configuration variables" and I need them to change sometimes (in my example, if I add a new language, I need to have a new language in the config it i18n library for CodeIgniter .

I need to install $languages and $special from the database.

 class MY_Lang extends CI_Lang { // languages private $languages = array( 'en' => 'english', 'sk' => 'slovak', 'fr' => 'french', 'nl' => 'dutch' ); // special URIs (not localized) private $special = array ( "admin", "link" ); . . . function MY_Lang() { parent::__construct(); . . . 

My thought is that I create a file and include it in the library.

As it should: I tried this, so the script will generate the language_config.php file every time the administrator speaks.

 class MY_Lang extends CI_Lang { public function __construct() { parent::__construct(); include_once(APPPATH.'/config/system_generated/language_config.php'); // languages $languages = $generated['languages']; // special URIs (not localized) $special = $generated['special']; } 

and generated file

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); // languages $generated['languages'] = array( 'en' => 'english', 'sk' => 'slovak', 'fr' => 'french', 'nl' => 'dutch' ); // special URIs (not localized) $generated['special'] = array ( "admin", "link" ); 

I do not ask how to generate the file, but how to include and use the included file inside the library file ( and set the variables as private ). I cannot set private variables inside the constructor , is there a way to set the included variables as private?

EDIT: SOLUTION

I forgot about private rules and in general OOP $this->... , the code below works fine.

 class MY_Lang extends CI_Lang { private $languages; private $special; public function __construct() { parent::__construct(); include_once(APPPATH.'/config/system_generated/language_config.php'); // languages $this->languages = $generated['languages']; // special URIs (not localized) $this->special = $generated['special']; } 

EDIT2: another issue with this

When I added the new __constructor() class to my class, this causes a problem, because for some reason it does not call __constructor() from CI_Lang even in my "added" __constructor() parent::__constructor(); which should call CI_Lang __construcotr() , but it is not. I don’t even know how to debug this.

SOLUTION EDIT2

There were 2 constructors in my code. Just combine them.

+4
source share
1 answer

Define them in the __construct() build __construct() , for example:

 private $languages; private $special; public function __construct() { $this->languages = $generated['languages']; $this->special = $generated['special']; } 
+5
source

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


All Articles