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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:20:26+00:00 2026-06-13T04:20:26+00:00

I have the following code: <?php class X { public function do($url) { $httpRequest

  • 0

I have the following code:

<?php

class X
{
    public function do($url)
    {
        $httpRequest = new \HttpRequest\Curl($url, $this->getOptions());
        $httpRequest->fire();
        // etc.
    }
    // ...
}

In order to be able to unit test this class, I’d like to inject a mocked HttpRequest class. One way to do this would be as follows:

<?php

class X
{
    private $httpRequestClass;

    public function __construct($httpRequestClass = '\HttpRequest\Curl')
    {
        $this->httpRequestClass = $httpRequestClass;
    }

    public function do($url)
    {
        $httpRequest = new $this->httpRequestClass($url, $this->getOptions());
        $httpRequest->fire();
        // etc.
    }
    // ...
}

But this doesn’t seem right. Any other ideas?

  • 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-13T04:20:27+00:00Added an answer on June 13, 2026 at 4:20 am

    The class needs to generate objects of type HttpRequest, but we don’t necessarily want it to initialize an object: we may want it to use the prototype pattern, for example. Therefore, the class calls for the factory pattern. I chose a factory callback, as opposed to a factory class, for brevity.

    <?php
    
    class X
    {
        private $factoryCallback;
    
        public function __construct($factoryCallback = null)
        {
            $this->factoryCallback = $factoryCallback;
        }
    
        public function do($url)
        {
            $httpRequest = $this->createHttpRequest($url);
            $httpRequest->fire();
            // etc.
        }
    
        private function createHttpRequest($url)
        {
            $callback = $this->factoryCallback;
            if (is_callable($callback)) {
                return $callback($url, $this->getOptions());
            }
            return new \HttpRequest\Curl($url, $this->getOptions());
        }
        // ...
    }
    

    The helper method, createHttpRequest(), is a bit redundant in this example, but would be used for error handling in production code.

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

Sidebar

Related Questions

I have the following code, <?php class Templater { static $params = array(); public
I have the following piece of code, which doesn't work: <?php class test{ public
I have the following code: $(document).ready(function() { $.getJSON('../includes/_quiz.php?class_id=163&course_id=183',function(data){ $.each(data,function(k,v){ questions[k] = v.question; answers[k*4] =
I have following PHP code $val=<div id=user.$row['cid']. userid=.$row['cid']. class=innertxt><img src=images/images.jpg width=50 height=50><strong>.$uname.</strong><ul> <li>Email: .$row['cemail'].</li>
I have the following PHP code; <?php component_customer_init(); component_customer_go(); function component_customer_init() { $customer =
The following code will have PHP unhappy that customMethod() is private. Why is this
I have the following code written, <?php require_once products.php; $test = new Products; $array
I have the following class code in PHP: <?php require_once CUSTOM_PATH . DIRECTORY_SEPARATOR .
I have the following model : class Model extends BaseModel { public function save($conn
i have following captcha code: <?php class CaptchaView extends View { private $fontsDir =

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.