(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