CI_Loader::library()

library($library[, $params = NULL[, $object_name = NULL]])

Parameters:
  • $library (mixed) – Library name as a string or an array with multiple libraries
  • $params (array) – Optional array of parameters to pass to the loaded library’s constructor
  • $object_name (string) – Optional object name to assign the library to
Returns:

CI_Loader instance (method chaining)

Return type:

CI_Loader

This method is used to load core classes.

Note

We use the terms “class” and “library” interchangeably.

For example, if you would like to send email with CodeIgniter, the first step is to load the email class within your controller:

$this->load->library('email');

Once loaded, the library will be ready for use, using $this->email.

Library files can be stored in subdirectories within the main “libraries” directory, or within your personal application/libraries directory. To load a file located in a subdirectory, simply include the path, relative to the “libraries” directory. For example, if you have file located at:

libraries/flavors/Chocolate.php

You will load it using:

$this->load->library('flavors/chocolate');

You may nest the file in as many subdirectories as you want.

Additionally, multiple libraries can be loaded at the same time by passing an array of libraries to the load method.

$this->load->library(array('email', 'table'));

Setting options

The second (optional) parameter allows you to optionally pass configuration setting. You will typically pass these as an array:

$config = array (
        'mailtype' => 'html',
        'charset'  => 'utf-8,
        'priority' => '1'
);

$this->load->library('email', $config);

Config options can usually also be set via a config file. Each library is explained in detail in its own page, so please read the information regarding each one you would like to use.

Please take note, when multiple libraries are supplied in an array for the first parameter, each will receive the same parameter information.

Assigning a Library to a different object name

If the third (optional) parameter is blank, the library will usually be assigned to an object with the same name as the library. For example, if the library is named Calendar, it will be assigned to a variable named $this->calendar.

If you prefer to set your own class names you can pass its value to the third parameter:

$this->load->library('calendar', NULL, 'my_calendar');

// Calendar class is now accessed using:
$this->my_calendar

Please take note, when multiple libraries are supplied in an array for the first parameter, this parameter is discarded.

doc_CodeIgniter
2016-10-15 16:31:45
Comments
Leave a Comment

Please login to continue.