Articles → PHP CODEIGNITER → Add And Override Functions In Existing Library In Codeigniter

Add And Override Functions In Existing Library In Codeigniter





  1. Add a new function in the existing library.
  2. Override the existing functions in the existing library.

Example - Add A New Function In The Existing Library








<?php
class My_Table extends CI_Table
{
    public function generateTable()
    {
        echo "This function will only display a message";
    }

}




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

$data = array(
array('Name', 'Color', 'Size'),
array('Fred', 'Blue', 'Small'),
array('Mary', 'Red', 'Large'),
array('John', 'Green', 'Medium')
);

echo $this->table->generate($data);

echo $this->table->generateTable();




Picture showing the output of generate function in codeigniter
Click to Enlarge


Example - Override Existing Functions In The Library




<?php
class MY_Table extends CI_Table
{
    public function generate($table_data = NULL)
    {
        echo "Calling generate";
    }
}




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

$table_data = array(
array('Name', 'Color', 'Size'),
array('Fred', 'Blue', 'Small'),
array('Mary', 'Red', 'Large'),
array('John', 'Green', 'Medium')
);

echo $this->table->generate($table_data);




Picture showing the output of overriden generate function in codeigniter
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Monday, July 27, 2020

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250