Inheritance in CodeIgniter - php

I am building a series of forms, and I am trying to inherit the functionality of a parent Form class into all the forms. For example,
LeaveForm extends Form (Model)
LeaveFormController extends FormController
I am handling all the leave form specific stuff in LeaveFormController and LeaveForm.
In LeaveFormController constructor, I simply call the parent class constructor, then load the LeaveForm Model. And in FormController constructor, I load Form model.
My problem is, I get an error,
Cannot redeclare class form in Form.php
Have I got my architecture wrong? How do I handle this ?

check if the class has already been initialized like this:
if (!class_exists('classname'))
{
// ok fine create new instance now
}

Possibly when you $this->load->model('Form'), you manually included the models/form.php file?
In your leaveform.php model file, make sure you load the superclass model you extend using codeigniter's model loading mechanism instead of require or include. Codeigniter has a loader that keeps track of already-loaded files to avoid redeclaring classes, but you need to use $this->load to use it. It won't know about files loaded directly with include or require.
So at the top of leaveform.php, use this:
$CI =& get_instance(); $CI->load->model('Form');
This is not related, but you will have pain unless you namespace your CodeIgniter model classes the same way you namespace Controller classes.
Try using FormModel extends CI_Model {}; Instead of Form extends CI_Model {};

Related

how to use another controller function without extends in our controller

how to use another controller function without extends in our controller
$this->load->library('../controllers/controllername');
already used
it is giving error =
Unable to locate the specified class: Session.php
Well you are not supposed to do that. If your controller uses repeatable logic, you should make class (Service for example), put the re-usable logic into it and call it in your controllers.
You can't use another controller function inside the controller. You can archive this in these two ways.
Create a Helper class
Create a generic model.

Extending own class in PHP CodeIgniter 3

I have two models: models/Categories.php and models/backend/BE_categories.php
I want BE_categories class to extend Categories, since it has some useful functions I can re-use:
Categories.php
class Categories extends CI_Model
{
...
BE_categories.php
class Be_categories extends Categories
{
...
But when $this->load->model('models/be_categories') I get the error: Unable to locate the specified class: Session.php yet it works with exending CI_Model What have I done wrong?
Just my guess, you load Session library inside some function in Categories model,and then try to use function with session library specific function inside model BE_Categories.
Add Session library in config/autoload.php or load it inside Categories model constructor, and remember to add constructor inside BE_Categories.
I also had a Controller called Categories.php, with class Categories, which is where CI was getting confused.
I had to rename models/Categories.php to models/Categories_model.php as well as the class name.
But also had to include the file when extending:
BE_categories_model.php
include('application/models/Categories_model.php');
class Be_categories_model extends Categories_model
{
...
Edit:
Thanks to commenter, might be better to to include this model in config/autoload.php instead of using PHP's include() function.

How to organize a Main controller for all the child controllers in codeigniter

I want to make something like this:
Main_Controller -> {
child1_controller
child2_controller
child3_controller
}
the purpose is to execute the constructor of the Main class everytime the child classes have been executed, it looks like some kind of multi extending, for example if i want to check in the main constructor wheter is admin or not, but the problem is when i do that
child1_controller extends Main_Controller
i get an error Class 'Main_Controller' not found
Use the built in core class extending mechanism, if you name your base controller class appropriately (with the default prefix MY_) and put it under application/core CI should pick it up.
Once you got your MY_Controller set up, you can make your child controllers under application/controllers/ extend that, and call for parent::__construct() in their __construct.

Load CodeIgniter models in Controller constructor

Coming from C#/.NET MVC 3/4, I'm not really used to CodeIgniter's implementation of models.
The documentation shows models being loaded within controller methods, however I'm using the model in almost every method and my model is storing data used across its methods in properties via its constructor.
Is there any reason NOT to instantiate the model in the controller constructor that I'm overlooking?
You can load model as per following ways also :
means if you have your model in any folder so using following code you can load model in controller.
$this->load->model('modelFolder/' . $this->controller . '_model');
For eg. :
if you have your model in folder named "modelFolder" then do like this :
class demoController extends CI_Controller {
var $controller = "user";
/* Local Constructor Will Be Overriding The One In The Parent Controller Class So We Need To Manually Call It. */
public function __construct() {
parent::__construct();
$this->load->model('modelFolder/' . $this->controller . '_model');
$this->load->model('common_model');
}
}
Hope it will help you...
There is no reason not to load the model for every controller activation. It could even be put in the configuration's autoload for all controllers.
The only reason not to always load it would be if many operations do not need the model. Then you could save a little bit of memory and time.

split extended classes over different files (php)

Szenario
I got a class as extend of an abstract class. The abstract class loads my files & extensions with their methods (link to pastebin for abstract class).
This gets called in my extends class like this (shortened & simplified - typos are only here at the Q-code):
class Pagination extends Pagination_Base
{
function __construct()
{
// loads the file "first.class.php" in the abstract class
parent::load_file( 'first' );
// stores the class as object in the abstract class, so we can access the methods and properties
parent::load_extension( new oxoFirst() );
}
}
Problem/Question
Now i got the error "Cannot redeclare class {$classname}".
I want to use my abstract class but still be able to move classes that contain extensions into separate files. Is there any way to do this?
Thanks in advance!
Use include_once instead of include in the load_file function.

Categories