Home » Php » php – Framework helpers, what are they for?

php – Framework helpers, what are they for?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I began looking at some frameworks. Particularly Code Igniter fascinated me. But searching trough the folders I found out system/helpers that contains 21 PHP files. Inside these files we find thousands of functions related to html, xml, strings, arrays, numbers, forms… etc.

This should be simple, but I really don’t understand the meaning of these helper files. I mean: What is generally considered an helper? Are they required? Could I delete them without risks?

How to&Answers:

A “helper” in many frameworks including CodeIgniter refers to a set of functions that make simple, routine tasks easier to accomplish by grouping multiple processes into a single function.

The official documentation for CodeIgniter helpers can be found here.

It provides the following explanation:

Helpers, as the name suggests, help you with tasks. Each helper file
is simply a collection of functions in
a particular category. There are URL
Helpers, that assist in creating
links, there are Form Helpers that
help you create form elements, Text
Helpers perform various text
formatting routines, Cookie Helpers
set and read cookies, File Helpers
help you deal with files, etc.

A list of helper functions and what they do can also be found in the documentation.

Dan

PS. You should not delete any files in the system directory as they may be relied upon within the core. You can create your own helpers which are placed in the application/helpers directory.

Answer:

Helpers are functions provided to make some tasks easier, like validation or input sanitizing. You shouldn’t delete them, and, in fact, should use them as much as possible, because they are usually well done and well tested.

Answer:

You should never delete files of a library/framework. They are designed to work “as a whole”. On the user side there are very less reason, where it make sense to cut a library/framework into pieces. So you will probably win nothing, but with the risk, that you break something.

However, “Helpers” is a generic concept, that provides some simple functionality, that another component may use (or not), without the need to directly implement it. Look at the codeigneter manual for some hints for which part this helpers are good for.

Answer:

you certainly shouldn’t delete any helper from the core/system files of a framework, as often the functionality will be used in other libraries and can cause all kinds of problems.

You don’t have to use them in your app, and they will cause no noticeable performance problems.

They may seem redundant at first, but the CI ones for instance are extremely helpful (as you’d probably expect).

You could argue that the form helper or the HTML helper just generates overhead, and you could type the appropriate HTML out yourself, but think:

   <form action="<?php echo site_url('controller/method'); ?>" method="post">

    // or using the form helper

    <?php echo form_open('controller/method');

Save your self all those extra characters, plus the risk of mispelling, mistyping etc.

Phil Sturgeon had a rant about the use of helpers and why they rock, which can be found here, raising several good points.

Edit

for the sake of proving my point!

$attributes = array('class' => 'email', 'id' => 'myform');
$hidden = array('hidden_value'=>'1234', 'hidden_value_2' => 'foobar');

echo form_open('email/send', $attributes, $hidden);