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

  • Home
  • SEARCH
  • 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 6381791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:26:24+00:00 2026-05-25T02:26:24+00:00

I have a real hard time understanding why the below code doesn’t work as

  • 0

I have a real hard time understanding why the below code doesn’t work as intended. I’ve usually use the bucket dependency injector, but to simplify, I’ve used twittee below, with the very same result.

So why aren’t my modified Config-object (and the created setting-element) available to the MockObject-class? From what I can tell, it’s being passed correctly.

Code:

$c = new Container();
$c->config = function ($c) { return new Config(); };
$config = $c->config;
$config->setting = 'a value';
$c->MockObject = function ($c) { return new MockObject($c->config); };
$c->MockObject; // Error thrown: `Notice: Undefined index: setting`

Classes:

class Container {
    protected $s=array();
    function __set($k, $c) { $this->s[$k]=$c; }
    function __get($k) { return $this->s[$k]($this); }
}

class Config {
    public $values = array();
    function __set($key, $value){ $this->values[$key] = $value; }
    function __get($key) { return $this->values[$key]; }    
}

class MockObject {
    function __construct(Config $config) { echo $config->setting; }
}

Dependency injection container courtesy of Fabien Potencier; https://github.com/fabpot/twittee

  • 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-05-25T02:26:25+00:00Added an answer on May 25, 2026 at 2:26 am

    I’m not extremely familiar with DI on PHP, but I think your problem is in this line:

    $c->config = function ($c) { return new Config(); };
    

    Your original code was

    $config = $c->config = function ($c) { return new Config(); };
    

    I’m guessing this threw an exception on $config->setting = 'a value'. $config and $c->config were both defined as a Closure that would return a new Config object. Since $config was a Closure, it would never have a setting property.

    Your updated code is

    $c->config = function ($c) { return new Config(); };  // $c->config is a closure as described
    $config = $c->config;  // $config is a new Config object
    

    Now $config is being defined as a Config class, not a Closure, so $configure->setting is valid.
    However, $c->config still refers to a Closure returning a new Config object. Since this new Config object doesn’t have a property named “Setting” it’s throwing an error when you try to retrieve it.

    I’m not sure how it fits with the rest of your design, but the following should work as expected:

    $c->MockObject = function ($c) { return new MockObject($config); };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do bubble sorts have any real world use? Every time I see one mentioned,
I haven't searched real hard recently but in the past I have searched high
So I have real time video stream. With 1 (one) person on It .
I have a real-time embedded app with the major cycle running at 10KHz. It
Does anyone know how sites that have a real-time feed of a lot of
I sometimes need to use Visual Studio when I have limited screen real estate
I'd like to start using unit tests, but I'm having a hard time understanding
I am having a real hard time finding a way to start, stop, and
I'm creating a Java swing app and I'm having a real hard time getting
Say I have real, dimension(0:100) :: realResults and I want to iterate over realResults

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.