CodeIgniter Forums
my controller fails to find helper function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: my controller fails to find helper function (/showthread.php?tid=92865)

Pages: 1 2


RE: my controller fails to find helper function - PaulC - 05-08-2025

(05-08-2025, 08:20 AM)paulbalandan Wrote: Ok, if all things are failing, then remove that use statement I suggested. Then, in your helpers, remove the "namespace App\Helpers;" line.

Bingo! But why ....


RE: my controller fails to find helper function - paulbalandan - 05-08-2025

When you declare a function in a file that has a namespace declaration like those you have in youre helpers, the full name of the function includes that namespace. So,
PHP Code:
// awesome_helper.php
namespace App\Helpers;

function 
whatever(): void {} 

In this case, your
Code:
whatever
 function's full name is
Code:
App\Helpers\whatever
. So, if you are invoking that function in your code, you usually add a use statement.

PHP Code:
// home controller
namespace App\Controllers;

use function 
App\Helpers\whatever// < this line

class HomeController extends BaseController
{
    public function foo(): void
    
{
        whatever(); // call your helper as usual
    }


If you don't have the namespace declaration in your helpers, then no need for the use statement line.


RE: my controller fails to find helper function - PaulC - 05-09-2025

(05-08-2025, 11:20 AM)paulbalandan Wrote: When you declare a function in a file that has a namespace declaration like those you have in youre helpers, the full name of the function includes that namespace. So,
PHP Code:
// awesome_helper.php
namespace App\Helpers;

function 
whatever(): void {} 

In this case, your
Code:
whatever
 function's full name is
Code:
App\Helpers\whatever
. So, if you are invoking that function in your code, you usually add a use statement.

PHP Code:
// home controller
namespace App\Controllers;

use function 
App\Helpers\whatever// < this line

class HomeController extends BaseController
{
    public function foo(): void
    
{
        whatever(); // call your helper as usual
    }


If you don't have the namespace declaration in your helpers, then no need for the use statement line.


OK Thx again.
I would simply add that when I added the use statement as you indicated it did not work.
Removing the namespace line did work! (Just in case anyone else trips over this)