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 8484539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:24:30+00:00 2026-06-10T20:24:30+00:00

Would this be considered dependency injection? <?php class BASE_Model extends CI_Model { /** *

  • 0

Would this be considered dependency injection?

<?php
class BASE_Model extends CI_Model 
{  
    /**
     * inject_class - load class using dependency injection
     *
     * @access public
     * @param  string $path
     * @param  string $class
     * @param  string $func
     * @param  string $method
     **/
    public function inject_class($path, $class, $func, $method)
    {
        // load_class is a function located in system/core/common.php on line 123
        $obj = load_class($class, $path, NULL);
        return $obj->$func();
    }
}

// lets say this is instantiated by a user controller when a new user is made
class User_model extends BASE_Model
{
    public function create()
    {
        echo 'create a new user';
        $request = $this->inject_class('path/to/models', 'Logger_model', 'log');
        echo $request;
    }
}

class Logger_model extends BASE_Model
{
    public function log()
    {
        return 'Logged';
    }
}
  • 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-10T20:24:32+00:00Added an answer on June 10, 2026 at 8:24 pm

    No. That’s just another way of how the object loads dependencies itself. The point of dependency injection is that every method/object/function takes its dependencies as arguments and does not load them itself in any way. User_model::create is injecting loading another class by itself, which is not accepting the dependency as an argument.

    The point of dependency injection is to reduce coupling. The User_model is now coupled to the Logger_model class, since it hardcodes the name and path to that specific class inside itself. If you wanted to use or test the User_model by itself in isolation, without it logging stuff you don’t want, you cannot do so anymore. Real DI would be this:

    public function create(Logger_model $log) {
        // here be dragons
        $log->log();
    }
    

    This way you can inject a mocked dummy logging class when you want to test the method without breaking anything or use alternative types of loggers when needed without needing to change any code.

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

Sidebar

Related Questions

Disclaimer: I would love to be using dependency injection on this project and have
Is it considered a bad practice to use optional parameters when using dependency injection
For a login system in php would this be a suitable outline of how
Would this be a proper way to dispose of a BackGroundWorker? I'm not sure
Would this be the correct way of declaring that an XML element Cluster contains
Why would this work timeout 10s echo foo bar # foo bar but this
In a single paged application, would this be the best way to bind and
I'd like to enable SVNAutoversioning for one repository only. Would this work? <Location /repos/>
I have read that you can do it, but would this really improve performance
I want to use dependency injection (Unity) and at the moment I'm thinking about

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.