Extending own class in PHP CodeIgniter 3 - php

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.

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.

Common functions in CodeIgniter

I am new to CodeIgniter.
I am using HMVC in CodeIgniter and want to use a module function in many other modules:
e.g I have a Locaton_model with function get_locations($param) { return; }
How do I use the above function in many other modules? Should I load the model in other module controllers every time I need this function or define the function some where globally?
You can easily achieve that by using core controllers:
http://ellislab.com/codeigniter/user-guide/general/core_classes.html
Instead of beginning your model with:
class Some_model extends CI_Model {}
You start with:
class Some_model extends MY_Model {}
Edit:
It is also possible to use libraries:
http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
This is useful when you want more general things, like a search engine, a IMAP interface, that kind of stuff.
if your creating your own model first make sure it is inside the core folder second on your controller extends the model name like this
class myController extends Locaton_model
{
function index()
{
$this->load->model->("your model name");
$this->yourmodelname->functionname($param);
}
}

Using the same function on two different controllers

I have two controllers on my website, let's call them MovieController and ReviewController.
What I want to do is to use one of MovieController's functions inside ReviewController.
The only thing I could think of is to extend MovieController instead of CController. However, it's hard for me to believe it's the right solution...
You have at least two options
1) Move the function to the main Controller, under components/Controller.php
class Controller extends CController {
2) Have another class extend from the above-mentioned main controller, and put your shared Movie/Review function inside it. Have both MovieController and ReviewController extend from this intermediate controller instead. Perhaps call it SharedController:
class SharedController extends Controller {
class MovieController extends SharedController {
class ReviewController extends SharedController {
You can:
1) create Helper class
2) use Traits/Behaviors
etc..

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.

Inheritance in CodeIgniter

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 {};

Categories