Articles → PHP CODEIGNITER → Override Existing Helper In Codeigniter

Override Existing Helper In Codeigniter





  1. Add a new function in the existing helper.
  2. Override the existing function with new functionality.



File Naming Convention




  1. Create a file in the "Helpers" folder.
  2. File naming convention should be "MY_helperclass_helper.php". For example if you want to override date helper then the file name should be "MY_date_helper.php".

Example - Add A New Function In The Existing Helper




$this->load->helper('date');

$bad_date = '199605';

$better_date = nice_date($bad_date, 'Y-m-d');

echo $better_date;




Picture showing the output of nice_date function
Click to Enlarge



<?php
function showmessage()
{
    echo "Calling showmessage";
}




$this->load->helper('date');

showmessage();




Picture showing the output of the function showmessage added in date helper
Click to Enlarge


Example - Override The Existing Function With New Functionality




function nice_date($date, $format)
{
echo "nothing";
}




$this->load->helper('date');

$bad_date = '199605';

nice_date($bad_date, 'Y-m-d');




Picture showing the output when nice_date function is overridden
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, July 23, 2020

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250