CodeIgniter Forums
Helpers and Other Conventions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Helpers and Other Conventions (/showthread.php?tid=65588)

Pages: 1 2 3 4 5 6 7


RE: Helpers and Other Conventions - kilishan - 06-29-2016

I don't know that the style guide is going to change at this point, honestly. While I didn't write the style guide, I think if you look at PHP's own functions, they're all named snake_case so, to me, it makes sense in that respect.

About namespacing, I'll consider it. I haven't done much work with namespacing and functions, but it seems like then everything gets even wordier? Instead of form_open(), you're now doing we'd have to use \CodeIgniter\Helpers\Forms\form_open(), which is kind of ugly. Smile I suppose we could put "use" statements in the views but that gets even uglier. So, not sure if there's an elegant way to make that work at the moment.


RE: Helpers and Other Conventions - prezire - 06-29-2016

There's a way to get around that. See my pre PHP7 sample http://icecream.me/2a74daa0983861570d3a50d0d07e4714. For reference, you may refer to http://php.net/manual/en/language.namespaces.importing.php and search for "Group use declarations". Hope it helps.

PS: Don't mind the require_once() and stuff Big Grin


RE: Helpers and Other Conventions - sv3tli0 - 06-29-2016

Too many namespaces within 1 framework and its app..
Perhaps all functions should be declared under CodeIgniter namespace only.


RE: Helpers and Other Conventions - prezire - 06-30-2016

(06-28-2016, 09:06 PM)kilishan Wrote: Here's the styleguide: https://bcit-ci.github.io/CodeIgniter4/contributing/styleguide.html

However, we don't have any plans at the moment to convert helpers to static classes. I've always been told that simply shoving a bunch of procedural functions into a class is "doing OOP wrong", so that's where my bias comes from. I will admit the syntax is, in some ways, nicer, and in other ways not to much. I can also see it being problematic with things like the form validation, if it stays anywhere close to how it is, and maybe some other things we have cooking.

(06-29-2016, 11:12 PM)sv3tli0 Wrote: Too many namespaces within 1 framework and its app..
Perhaps all functions should be declared under CodeIgniter namespace only.

I believe the goal is to avoid name conflicts.


RE: Helpers and Other Conventions - sv3tli0 - 06-30-2016

(06-30-2016, 12:11 AM)prezire Wrote: I believe the goal is to avoid name conflicts.

Its good goal but 1 avoid of conflicts should not create others..
Helper is too common word as Config which is used already for namespace.. 
And having CodeIgniter, App, Config, Helper namespaces just within 1 sample app its too much I think..

At least for Functions I don't see any problem to have them just under CodeIgniter namespace. 
Perhaps some system option to alias them into the APP namespace can be helpful.

By the way many calls of USE (in controllers / models / views ) is something common this days. 
My IDE PHPstorm even auto add it at the start of the file when you are using some class/function under some namespace..

So this is the way how PHP is made and developers must accept it I think.


RE: Helpers and Other Conventions - jlp - 06-30-2016

Without getting hungup on specific names, etc, it sounds like something along the lines of the following might be a good approach. Thoughts?

CodeIgniter3 ... 


system/helpers/url_helper.php
Code:
if ( ! function_exists('site_url'))
{
    function site_url($uri = '', $protocol = NULL)
    {
        return get_instance()->config->site_url($uri, $protocol);
    }
}
...

application/whatever...
Code:
$this->load->helper('url');
echo site_url();

CodeIgniter4?? ...

system/helpers/URL.php
Code:
namespace CodeIgniter\Helpers\URL;

    function siteURL($uri = '', $protocol = NULL)
    {
        return ...
    }
...

application/whatever...
Code:
use CodeIgniter\Helpers\URL;
...
echo URL\siteURL();



RE: Helpers and Other Conventions - Narf - 06-30-2016

(06-29-2016, 06:13 AM)kilishan Wrote:
(06-29-2016, 01:17 AM)Narf Wrote: It's not OOP at all, you're not using objects.
The only reason for that usage is lack of function autoloading.

Sure it is. Classes are objects, and it's got methods. That makes it a (really poorly designed) object, right? Smile But, yes, I know what you mean.

The other valid reason I've heard for doing that is it limits the global function pollution. Neither one of those are good enough reasons for me to consider the change, though.

No ... classes are classes. When you don't instantiate them, there are no objects.


RE: Helpers and Other Conventions - sv3tli0 - 06-30-2016

(06-30-2016, 12:53 AM)jlp Wrote: application/whatever...
Code:
use CodeIgniter\Helpers\URL;
...
echo URL\siteURL();

This is most comfortable way.  Idea

Controller/Model/View there is no difference, such usage is normal for any PHP code..
And there is no reason why namespaces usage shouldn't exists in a view file, at least until this view is a PHP file.


RE: Helpers and Other Conventions - prezire - 06-30-2016

I like it too. But reserved words are not allowed like below:

PHP Code:
use Helpers\Array;
Array\
elements();
Array\
randomElement(); 

I know you guys can find a way Smile


RE: Helpers and Other Conventions - Narf - 06-30-2016

(06-30-2016, 05:21 AM)prezire Wrote: I like it too. But reserved words are not allowed like below:

PHP Code:
use Helpers\Array;
Array\
elements();
Array\
randomElement(); 

I know you guys can find a way Smile

The array helpers are useless.