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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:44:22+00:00 2026-06-17T16:44:22+00:00

As I use OO design patterns I want to make sure I am really

  • 0

As I use OO design patterns I want to make sure I am really getting it. I understand about the importance of dependency injection and also about service container/factory objects. I love the idea of a factory method that can inject dependencies into itself when loaded via a static method, returning a copy of itself that is complete. I like how clean this is in the code that consumers the objects. And, in testing, you can inject different objects instead (overwrite, or instantiate without using the factory method, see below)

Is there anything about the following code that raises alarm bells? Am I understanding this correctly?

abstract class AbstractClass
{
    public function __construct ()
    {

    }

    public static function factory ()
    {
        throw new Exception ('Please create a concrete class version of the method ' . __FUNCTION__);
    }

    public function inject ($class, $className=null)
    {

        if ($className === null)
        {
            $className          = get_class ($class);
        }

        $this->{$className} = $class;
    }
}

class ConcreteClass extends AbstractClass
{
    public static function factory ()
    {
        $me = new self;
        $me->inject (RebarClass::factory ());
        $me->inject (AsphaltClass::factory ());
        $me->inject (CementClass::factory ());

        return $me;
    }

    public function doSomething ()
    {
        echo $this->RebarClass->doSomethingCool ();
    }

}

class RebarClass extends AbstractClass
{
    public static function factory ()
    {
        return new self;
    }

    public function doSomethingCool ()
    {
        return "I did something, but it wasn't that cool...\n";
    }
}

class AsphaltClass extends AbstractClass
{
    public static function factory ()
    {
        return new self;
    }
}

class CementClass extends AbstractClass
{
    public static function factory ()
    {
        $me = new self;
        $me->inject (AsphaltClass::factory ());
        $me->inject (SandClass::factory ());

        return $me;
    }
}

class SandClass extends AbstractClass
{
    public static function factory ()
    {
        return new self;
    }
}

To me, this gives me a lot of flexibility when I’m creating and using objects in controllers and other models, I can instantiate like:

$concreteClass = ConcreteClass::factory ();

And now my object is set up the way I want

print_r ($concreteClass);
echo "\n";

Outputs:

ConcreteClass Object
(
    [RebarClass] => RebarClass Object
    (
    )

    [AsphaltClass] => AsphaltClass Object
    (
    )

    [CementClass] => CementClass Object
    (
        [AsphaltClass] => AsphaltClass Object
        (
        )

        [SandClass] => SandClass Object
        (
        )

    )

)

And internally the other objects are easy to use

echo $concreteClass->doSomething ();

And, if you want to use this for unit testing, you can do either:

$concreteClass = ConcreteClass::factory ();
$concreteClass->inject(new DifferentAsphaltClass, 'AsphaltClass'); // overwrite

OR

$concreteClass = new ConcreteClass; // now you are responsible for setting up dependencies yourself
$concreteClass->inject (new YetAnotherAsphaltClass, 'AsphaltClass');
  • 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-17T16:44:23+00:00Added an answer on June 17, 2026 at 4:44 pm

    The presented approach has some risks related to properly injecting the required class if not using the factory.

    The code is also a little bit harder to follow due to the unavailability of the list of required injections.

    I would suggest using the class constructor as the receiver of dependencies instead of using the inject method.

    class Concrete {
        public static function Factory() {
            $rebar = Rebar::Factory();
            $asphalt = Asphalt::Factory();
            $sand = Sand::Factory();
    
            return new Concrete($rebar, $asphalt, $sand);
        }
    
        public function __construct(IRebar $rebar, IAsphalt $asphalt, ISand $sand) {
            $this->Rebar = $rebar;
            $this->Asphalt = $asphalt;
            $this->Sand = $sand;
        }
    }
    

    The IRebar, IAsphalt, and ISand could be interfaces ( http://php.net/manual/en/language.oop5.interfaces.php) .

    interface IRebar {
    
    }
    
    class MyRebar implements IRebar {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have studied design patterns and want to use them to code an open
I'm working my way through 'head first design patterns' and want to use this
I am a beginner in design patterns. I am trying to use Abstract Factory
I have been meaning to make use of design pattern in PHP, such as
I'm developing a webapp and I'm not sure which pattern design should I use.
I'm starting to read about design patterns and trying to apply them to some
Please share your favorite application design / design patterns for use in PHP with
So I'm learning about design patterns in school. Today I was told about the
I am learning java. I am trying to use composite design pattern. I am
What is a design pattern I can use for generating context-sensitive right click menus

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.