Welcome Guest, Not a member yet? Register   Sign In
Helpers, Libraries, and where to load them..
#2

[eluser]bretticus[/eluser]
A library is a stand-alone PHP class (related functions and variables; or methods and properties to use OOP terms.) Helpers are typically a collection of related simple functions that do not need to be loaded as an object and can stand by themselves. This is basically the difference between an object method(s) and a function (See PHP Classes and Objects.)

In classes, you can call the constructor (in PHP4 it's a method by the same name as the class. In PHP5 you can simply call function __construct().) This constructor method is called each time the class is instantiated (the same as setting a variable to a new class which CI does automatically via URL path. i.e: "Convention over configuration.") The index() method is just the default method for the controller class in CodeIgniter. Loading from here doesn't load globally for the entire Controller class. In other words, use the constructor instead. If you want the same helpers and libraries loaded in all your controllers, there are two options:

1. load from the constructor in a MY_Controller and extend all subsequent Controllers from that.

2. Or, the easiest method, load from the application/config/autoload.php file.

It's okay to load libraries, etc over and over again in different controllers and controller methods. One advantage is that your current method gets only the resources it needs which results in better overall performance. Of course, if you find yourself using a function from a helper or library over and over again, autoload it.


Messages In This Thread
Helpers, Libraries, and where to load them.. - by El Forum - 08-25-2010, 01:31 PM
Helpers, Libraries, and where to load them.. - by El Forum - 08-25-2010, 02:01 PM
Helpers, Libraries, and where to load them.. - by El Forum - 08-25-2010, 02:11 PM
Helpers, Libraries, and where to load them.. - by El Forum - 08-25-2010, 02:24 PM
Helpers, Libraries, and where to load them.. - by El Forum - 08-25-2010, 07:13 PM
Helpers, Libraries, and where to load them.. - by El Forum - 08-25-2010, 10:12 PM
Helpers, Libraries, and where to load them.. - by El Forum - 08-26-2010, 07:10 AM
Helpers, Libraries, and where to load them.. - by El Forum - 08-26-2010, 07:25 AM
Helpers, Libraries, and where to load them.. - by El Forum - 08-26-2010, 07:30 AM
Helpers, Libraries, and where to load them.. - by El Forum - 08-26-2010, 07:35 AM



Theme © iAndrew 2016 - Forum software by © MyBB