CodeIgniter Forums
DEPRECATED on construct? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: DEPRECATED on construct? (/showthread.php?tid=92273)



DEPRECATED on construct? - 4usol - 01-04-2025

I see the following infos in the debug-toolbar

"[DEPRECATED] Creation of dynamic property App\Libraries\MyLib::$ModulesModel is deprecated in APPPATH/Libraries/MyLib.php on line 128. 1 APPPATH/Controllers/BaseController.php(71): App\Libraries\MyLib->__construct() 2 SYSTEMPATH/CodeIgniter.php(904): App\Controllers\BaseController->initController(Object(CodeIgniter\HTTP\IncomingRequest), Object(CodeIgniter\HTTP\Response), Object(CodeIgniter\Log\Logger)) 3 SYSTEMPATH/CodeIgniter.php(500): CodeIgniter\CodeIgniter->createController() 4 SYSTEMPATH/CodeIgniter.php(355): CodeIgniter\CodeIgniter->handleRequest(null, Object(Config\Cache), false) 5 SYSTEMPATH/Boot.php(325): CodeIgniter\CodeIgniter->run() 6 SYSTEMPATH/Boot.php(67): CodeIgniter\Boot::runCodeIgniter(Object(CodeIgniter\CodeIgniter)) 7 FCPATH/index.php(56): CodeIgniter\Boot::bootWeb(Object(Config\Paths))"

So have a look at the line i found this

Code:
//Construct
    function __construct()
{
        //Session
        $this->session = session(); 


        //Models
        $this->ModulesModel = model('ModulesModel');  //This is line 128
        $this->UsersModel = model('UsersModel');
        $this->RolesModel = model('RolesModel');
        $this->CompanyModel = model('CompanyModel');
       
        //Helper
        $this->current_url = current_url(true);

        ...
    }

What is wrong with this, and how i can do it, may i dont need to create a own instance of each needed modell in the library!?


RE: DEPRECATED on construct? - JustJohnQ - 01-04-2025

You need to declare the property, add the following lines above your __construct():

PHP Code:
private object $ModulesModel;
private 
object $UsersModel;
private 
object $RolesModel

Also, read this: https://php.watch/versions/8.2/dynamic-properties-deprecated


RE: DEPRECATED on construct? - 4usol - 01-04-2025

thats it - thank you!