Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:34:01+00:00 2026-06-15T15:34:01+00:00

I am using CodeIgniter and have extended CI_Model . so all my models now

  • 0

I am using CodeIgniter and have extended CI_Model. so all my models now extend MY_Model.

This works fine.

Issue is that all my models have a secondary associated object. basically a class that gets passed data from the model (usually from the database) and represents that row in the database.

so something like

class Product_Model extends MY_Model{
    public function get($id){
        //....
        return new Product($query->row()); 
    }
}


class Product{

    public function __construct(stdClass $data){
      //....
      self::$ci =& get_instance();
      self::$model = self::$ci->products;
    }

}

Now I load the Product_Model with an alias $this->load->model('product_model', 'products');

Hence having self::$model = self::$ci->products;

But now I want to have a basic class that all the classes like Product will extend.

I want this to contain the logic to update self::$model.

But I need to know the models alias.

Something like

self::$model = self::$ci->{instantiator_variable_name($this)} which would be self::$model = self::$ci->products

Now obviously that function does not exist but it shows what I want to do.

I know I could for everywhere that I create the Product or similar have

$row = $query->row();
$row->model = $this->ci->products;
return new Product($row);

But I would rather automate it if I could.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:34:02+00:00Added an answer on June 15, 2026 at 3:34 pm

    It might help if you clarify the situation a bit. Post a bit more of your code please?

    For example, Modals (in CodeIgniter) are generally used as singleton classes which (almost) explains using ‘self::” but it looks like you want Product to be an Object. So why does that use

    self::$model 
    

    instead of

    $this->model
    

    The fact that you’re aliasing the products model makes me think you might be doing this on purpose (which is why I’m confused, why would you do this?). I think you should review the difference between “self::”, “static::”, and “$this->”. Take a look at http://php.net/manual/en/language.oop5.late-static-bindings.php

    rockstarz is correct, you need to use the Factory Pattern. Consider something like this:

    class ItemFactory {
    
        private $model;
    
        public function __construct($model) {
            $this->model = $model;
        }
    
        function create_product(stdClass $data) {
            $product = new Product($data);
            $product->set_model($this->model);
            return $product
        }
    }
    
    abstract class Item {
    
        protected $model;
        protected $ci = & get_instance();
    
        public function __construct(stdClass $data) {
            // whatever
        }
    
        public function set_model($model) {
            $this->$model = $model;
        }
    
        public function get_model() {
            return $this->model;
        }
    
    }
    
    class Product extends Item {
        // whatever
    }
    

    Then your model can just use it like

    class Product_Model extends MY_Model {
    
        private $item_factory;
    
        public function __construct() {
            $this->item_factory = new ItemFactory($this);
        }
    
        public function get($id){
            return $this->item_factory->create_product($row);
        }
    
    }
    

    Relevant reading materials:

    http://en.wikipedia.org/wiki/Inversion_of_control#Implementation_techniques

    http://en.wikipedia.org/wiki/Factory_method_pattern

    http://en.wikipedia.org/wiki/Dependency_injection

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function on my controller (Im using CodeIgniter) that reads the database,
In my LAMP application (using CodeIgniter), I have a call to $this->db->update... that generates
I'm using codeigniter I have this BuildTemplate function in my [HOME] controller that authenticates
I have a site using CodeIgniter that is almost complete now. My problem is
I have been using CodeIgniter all these while, and have solid foundation in OOP
Using Codeigniter, I have a controller function name search that takes in parameters state
I'm using CodeIgniter 2 (latest build) ...let's say I have this query: $CI->db->select(FileName, GUID,
Using codeigniter 2.1.2, I have a form view loaded through a controller that takes
I am using codeigniter, I have URL like this ( http://localhost/en/home/submit ). My App
i have a form that uses a drop down and im using codeigniter ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.