Welcome Guest, Not a member yet? Register   Sign In
Where to put standard functions that reference lib/mod methods
#1

I was curious, if I wanted to create basic functions that would reference functions inside a library or model, where would be the best place to put it? Just below the class or inside a helper?

I know its common to just throw it below the class, (EG: http://pastebin.com/MGvk4xy4) But I was wondering, (per CI best practices) should these go in a helper instead?

Thanks
Reply
#2

(This post was last modified: 08-10-2015, 11:59 PM by Wouter60.)

You can make your own MY_Controller and MY_Model.

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
MY_Controller extends CI_Controller {

function 
__construct()
{
  parent::__construct();
}

//your basic functions here


PHP Code:
class MY_Model extends CI_Model {

function 
__construct()
{
  parent::__construct();
}

//your basic functions here



Save both files in the application/core folder.

Now, when you create a controller that needs the basic functions in your MY_Controller, you start it like in the example below:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Home extends MY_Controller {



And the same goes for models that are based upon your own MY_Model.
Reply
#3

A helper would be the best practice in this case. The helper functions should load the library as needed via
Code:
get_instance()->load->library('YourLibrary');

The only advantage to putting the helper functions at the end of the library's file is that it's somewhat safer to assume the library has already been loaded and the end user doesn't have to load the helper if they've already loaded the library.

Personally, I would prefer not to have helper functions loaded every time I load a library. Bonfire has a few libraries that do this, and I've been considering adding some configuration options to make it possible to separate the libraries from their helpers while maintaining backwards compatibility. Once you get to that point, though, the formula for weighing the advantages against the disadvantages is pointless, because the cost of checking for an additional config value and choosing not to load a helper file is probably pretty close to the cost of loading a small number of extra functions after the end of the class declaration in the library, except, of course, for the general costs associated with declaring global functions in the first place.
Reply
#4

IMO, it's a sign of bad design when you start mixing procedural functions and OOP code in the way that you're trying to. If I ever needed anything similar, I'd just declare static methods in the class instead.
Reply
#5

(08-10-2015, 11:57 PM)Wouter60 Wrote: You can make your own MY_Controller and MY_Model.


PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
MY_Controller extends CI_Controller {

function 
__construct()
{
  parent::__construct();
}

//your basic functions here


PHP Code:
class MY_Model extends CI_Model {

function 
__construct()
{
  parent::__construct();
}

//your basic functions here



Save both files in the application/core folder.

Now, when you create a controller that needs the basic functions in your MY_Controller, you start it like in the example below:

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Home extends MY_Controller {



And the same goes for models that are based upon your own MY_Model.
Wait.. How would this help though? I appreciate your input, but how does this help?



(08-11-2015, 06:18 AM)mwhitney Wrote: A helper would be the best practice in this case. The helper functions should load the library as needed via
Code:
get_instance()->load->library('YourLibrary');

The only advantage to putting the helper functions at the end of the library's file is that it's somewhat safer to assume the library has already been loaded and the end user doesn't have to load the helper if they've already loaded the library.

Personally, I would prefer not to have helper functions loaded every time I load a library. Bonfire has a few libraries that do this, and I've been considering adding some configuration options to make it possible to separate the libraries from their helpers while maintaining backwards compatibility. Once you get to that point, though, the formula for weighing the advantages against the disadvantages is pointless, because the cost of checking for an additional config value and choosing not to load a helper file is probably pretty close to the cost of loading a small number of extra functions after the end of the class declaration in the library, except, of course, for the general costs associated with declaring global functions in the first place.
Thats what I thought, thanks!


(08-11-2015, 07:39 AM)Narf Wrote: IMO, it's a sign of bad design when you start mixing procedural functions and OOP code in the way that you're trying to. If I ever needed anything similar, I'd just declare static methods in the class instead.
I kinda agree, but sometimes its easier to do something like...
echo "Your id is " . un_from_id; 
as opposed to 
echo "Your id is " . $this->Accounts_model->username_from_id('123'); 
every time. I thought that it was a no-no to add procedural functions, But ive seen it done quite a few times, including in WP, so oh well
Reply
#6

(08-11-2015, 11:07 AM)jLinux Wrote:
(08-11-2015, 07:39 AM)Narf Wrote: IMO, it's a sign of bad design when you start mixing procedural functions and OOP code in the way that you're trying to. If I ever needed anything similar, I'd just declare static methods in the class instead.
I kinda agree, but sometimes its easier to do something like...
echo "Your id is " . un_from_id; 
as opposed to 
echo "Your id is " . $this->Accounts_model->username_from_id('123'); 
every time. I thought that it was a no-no to add procedural functions, But ive seen it done quite a few times, including in WP, so oh well

I suggested static methods, you call those directly as className::methodName(). Smile

And you're going to see A LOT of crappy code from all kinds of sources, don't take that as a validation of bad practices ... especially WP is probably the worst source of inspiration in that regard.
Reply
#7

Quote:Wait.. How would this help though? I appreciate your input, but how does this help?
Well, it would avoid duplicating the same code in multiple controllers and/or models.
Narf's suggestion for using static methods is a good alternative also.
But if your main goal is to make your function calls shorter (and perhaps easier to read), then a seperate helper would be the best solution (IMHO). That's the way CI itself is solving this.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB