CodeIgniter Forums
Does CodeIgniter Automatically instantiate all classes in a file?? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Does CodeIgniter Automatically instantiate all classes in a file?? (/showthread.php?tid=62365)



Does CodeIgniter Automatically instantiate all classes in a file?? - eldan88 - 07-06-2015

Hey Guys,

I have a quick question. I am trying to build an abstract factory pattern. The files I am working with has two classes that shouldn't be instantiated.

If i have a file names test.php and have two classes

class Test1{

}

class Test2{

}

and then lets say I call this->load->library("test");

Will it automatically instantiate Test1 and Test2 classes?


RE: Does CodeIgniter Automatically instantiate all classes in a file?? - StratoKyke - 07-06-2015

I think you should create a file for each class. Infact if you check in the system each file have a single class.


RE: Does CodeIgniter Automatically instantiate all classes in a file?? - mwhitney - 07-06-2015

It is recommended that each file only contain one class, and that the name of the file and class match. However, if you ignore that and add any additional classes to the file, they will be included, but not instantiated. The loader doesn't instantiate classes it isn't told to load, even if they are included in the same file as a class it is told to load.

Additionally, a library's class and file name should start with a capital letter (the loader applies ucfirst() to the first parameter passed to the library() method, so you can call $this->load->library('test'), but the file should be named Test.php, and it should contain a class named Test). An all-lowercase file name may work in development environments that don't have case-sensitive file systems, but most server environments do use case-sensitive file systems. CI2 might work with either name for a library, but CI3 won't.


RE: Does CodeIgniter Automatically instantiate all classes in a file?? - eldan88 - 07-18-2015

(07-06-2015, 09:38 AM)mwhitney Wrote: It is recommended that each file only contain one class, and that the name of the file and class match. However, if you ignore that and add any additional classes to the file, they will be included, but not instantiated. The loader doesn't instantiate classes it isn't told to load, even if they are included in the same file as a class it is told to load.

Additionally, a library's class and file name should start with a capital letter (the loader applies ucfirst() to the first parameter passed to the library() method, so you can call $this->load->library('test'), but the file should be named Test.php, and it should contain a class named Test). An all-lowercase file name may work in development environments that don't have case-sensitive file systems, but most server environments do use case-sensitive file systems. CI2 might work with either name for a library, but CI3 won't.

Alright got it! Thanks a lot mwhitney