Codeigniter - SPLessons

CodeIgniter Core System Classes

Home > Lesson > Chapter 15
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Core System Classes

CodeIgniter Core System Classes

shape Description

CodeIgniter Core System Classes are used to allow own versions of class definitions and extensions. Original core classes of CodeIgniter can be swapped with the own versions or else core versions can be extended. Core System classes will not be used mostly except in the situations when required own significance. Some of the CodeIgniter Core System Classes are:

Creating Core System Classes

shape Description

Open the file path application/core and create the file named "Splessons.php" (If the folder "core" is not present, it can be created manually) and enter the below code. [php] class CI_Splessons { //statements } [/php]

How to extend core Class

Core System classes in CodeIgniter can be extended or swapped with own versions in 3 different ways.
  1. Own class should extend the parent class of CodeIgniter. For example, to extend a controller use extend CI_Controller as a parent class.
  2. Class type should be headed by a prefix like SP_. For example, CI_ can be replaced with SP_.
  3. The file name should be same as the class name. For example, if the controller name is Sp_Controller then the file name should be Sp_Controller.php.

shape Example

Creating Core System Classes

Create a controller file "SP_Controller" in the core folder present in the path folder application/core. [php] class SP_Controller extends CI_Controller { public function __construct() { parent::__construct(); } } ?> [/php] In the above code, it can be understood that SP_Controller is the own version which extends the parent controller CI_Controller. Then create a Model in the same core folder present in application/core directory. [php] class SP_Model extends CI_Model { public function __construct() { // Call the CI_Model constructor parent::__construct(); } } ?> [/php]

Using Core System Classes

Now the above created own system classes are used as base system classes in other models and controllers. Create a model Sample that extends the class SP_Model. [php] <?php class Sample extends SP_Model { public function getModel() { echo "<h1>This is Model Function</h1>"; } } ?> [/php] Then create a controller Sppage that extends the class SP_Controller. [php] <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Sppage extends SP_Controller { public function index() { $this->load->view('welcome_message'); } public function spmodel() { $this->load->model('Sample'); //calling model $this->Sample->getModel(); } } [/php] Output: When searched with the url localhost/Codeigniter/index.php/sppage the output will be as follows. To see the output of the created example, enter the url localhost/Codeigniter/index.php/sppage/spmodel.

Summary

shape Key Points

  • CodeIgniter Core System Classes are used to allow own versions of class definitions and extensions.
  • CodeIgniter Core System Classes - In application/core folder, system classes will be created.