CodeIgniter Forums
CI4 controller error with built-in php class (intl) - 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 controller error with built-in php class (intl) (/showthread.php?tid=77081)



CI4 controller error with built-in php class (intl) - nmaa3003 - 07-17-2020

this my code

PHP Code:
46     public function view_receipt($id)
47     {
48         $data['receipt'] = $this->company_model->get_receipt($id);
49         $data['receipt']->description explode('^'$data['receipt']->description);
50         $data['receipt']->price explode('^'$data['receipt']->price);
51         $data['receipt']->quantity explode('^'$data['receipt']->quantity);
52         $data['company'] = $this->company_model->get_my_company();
53         $credit_term = new NumberFormatter("en"NumberFormatter::SPELLOUT);
54         $data['credit_term'] = $credit_term->format($data['receipt']->credit_term);
55         echo view('templates/receipt'$data);
56     

at line 53 i try to use NumberFormatter class which is built-in php by enabling intl extension.
working fine in php CLI.

but issue an error when use it in controller.


Quote:Error
Class 'App\Controllers\NumberFormatter' not found


APPPATH\Controllers\Home.php at line 53



RE: CI4 controller error with built-in php class (intl) - jreklund - 07-17-2020

Add \ in front of it to use native PHP-functions.

\NumberFormatter


RE: CI4 controller error with built-in php class (intl) - nmaa3003 - 07-17-2020

(07-17-2020, 02:44 AM)jreklund Wrote: Add \ in front of it to use native PHP-functions.

\NumberFormatter
thanks a lot.

as far i remember, in ci3 we dont hv to put it.

*note to other:
make sure to put \ to both class name like this -> $credit_term = new \NumberFormatter("en", \NumberFormatter::SPELLOUT);


RE: CI4 controller error with built-in php class (intl) - jreklund - 07-17-2020

That's correct. CI3 don't use namespaces, so everything are global. Now it will try finding the closes match first.


RE: CI4 controller error with built-in php class (intl) - tgix - 07-17-2020

Just to add:
a good IDE will provide you with tons of help when it comes to namespacing stuff.