CodeIgniter Forums
Class alias and changing helpers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: Class alias and changing helpers (/showthread.php?tid=79228)



Class alias and changing helpers - iRedds - 05-14-2021

I suggest considering the possibility of setting up class aliases and replacing the helpers from functions with class methods.
This will simplify the use of helpers and allow them to be extended.
Example
PHP Code:
//Autoload config

$alias = [
    'Html' => \CodeIgniter\Helpers\Html::class,
    'Arr' => \CodeIgniter\Helpers\Array::class,
];

// Html class

class Html 
{
    public static function anchor()
    {
        return '....';
    }

    public static function link()
    {
        return '....';
    }    
}

// using  

Html::anchor();


// extends 

$alias = [
    'Html' => \App\Helpers\Html::class,
];

// Html class

class Html extends \CodeIgniter\Helpers\Html 
{
    public static function anchor()
    {
        return '....';
    }

  • Direct use in templates without preloading via the helper() function
  • Using protected methods instead of prefixed functions. Like _some_protected_function()
  • Resolving possible conflicts in the names of functions of external libraries.



RE: Class alias and changing helpers - InsiteFX - 05-15-2021

I have to agree on this also, would make it a lot simpler to use and maintain.