Welcome Guest, Not a member yet? Register   Sign In
Does CodeIgniter Automatically instantiate all classes in a file??
#1

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?
Reply
#2

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

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.
Reply
#4

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB