CodeIgniter Forums
CI4 : I try to add a function in a helper - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: CI4 : I try to add a function in a helper (/showthread.php?tid=78280)



CI4 : I try to add a function in a helper - kilden - 12-28-2020

Hello,

Following the documentation I've tried for the first time to extend an helper...
So I've created a file 'text.php' (with the same name as the helper 'text') like this :


Code:
<?php
function format_price($number){
  return (substr($number, -2)==00)  ? substr($number, 0, -3).'.-' : sprintf("%.2f",($number));
}
?>



I put it in app/Helpers, but when I tried to call it in a Controller :


Code:
helper('text');
$data['price'] = format_price(45);


I've got an error like this (strangely with no controller's name) :
Call to undefined function App\Controllers\ctrl_web\format_price()


I don't understand what I do wrong... I've tried to create a new helper ('text2.php') without success, too.
Thank you for your lights and I wish you a merry Christmas and a happy new year !


RE: CI4 : I try to add a function in a helper - natanfelles - 12-28-2020

Name the file as "text_helper.php"


RE: CI4 : I try to add a function in a helper - kilden - 12-28-2020

oh... ! I feel so stupid... hum hum... Well thank you very much ! It was so simple ! :-)