CodeIgniter Forums
how often will public function __construct() run? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: how often will public function __construct() run? (/showthread.php?tid=78835)



how often will public function __construct() run? - richb201 - 03-16-2021

In the top of my controller i have 

public function __construct()

In it I initialize the database, load helpers and libs, get environment variables, and then runs $this->_init(). It seems that _construct (and thus _init) is running multiple times. In init() I initialize fusionAuth. Thus I am doing it multiple times per user. I have have seen this through use of the debugger. For example, every time new method in the controller is called. 

Is this correct? where should I be initializing "system wide" resources?


RE: how often will public function __construct() run? - kenjis - 03-16-2021

I believe the constructor is called once in a request.


RE: how often will public function __construct() run? - richb201 - 03-17-2021

Thanks. In that case, where should I place "run-once" type activities, such as opening databases and loading session variables? BTW, I am getting up to the point where I am going to need to fully test my application and have never set up testing before. Now I know there are different types of testing (over 20 different kinds). What type of testing does your book address? Also, I am having trouble with Composer. Does that preclude using your testing procedure?


RE: how often will public function __construct() run? - kenjis - 03-17-2021

PHP is shared nothing architecture.
There is no "run-once" type activities.

> What type of testing does your book address?

My book explains how to write PHPUnit test code, and
Codeception acceptance testing for CodeIgniter 3.x.

> I am having trouble with Composer. Does that preclude using your testing procedure?

I don't know. But the book is not about Composer.

And the book is a bit out of dated. You can learn testing
concepts and basic knowledge, but you will need to refer
to the manual of each tool to build the latest development
environment.

Software in the book:
• PHP 5.5 (You can use PHP 5.4 or 5.6)
• CodeIgniter 3.0
• ci-phpunit-test 0.10
• PHPUnit 4.8
• Codeception 2.1
• Selenium Standalone Server 2.48