Factory style pattern in method I have a method in a class, and its sole purpose is to prepare another class for use:

private function createHttpPost($endPoint) {
    $httpPost = new HttpPost($this-&gt;baseUri . '/' . $endPoint);
    $httpPost-&gt;setContentType('application/json');
    $httpPost-&gt;setAccept('application/json');

    if($this-&gt;auth) {
        $this-&gt;authenticate();
        $httpPost-&gt;addCookies(array($this-&gt;sessionName =&gt; $this-&gt;sessionId));
        $httpPost-&gt;addHeaders(array('X-CSRF-Token' =&gt; $this-&gt;token));
    }

    return $httpPost;
}


I am familiar with the factory design pattern, but I'm not sure it is a good idea to apply that kind of thinking to a class method. Should I take this code out and put it in a new class called JsonHttpPostFactory, or something like that?

Is this a bad idea?  If so, why?
In itself, there's nothing wrong with this code. There really isn't. But there are some things you need to consider:


The method is private, so it's an internal kind of factory, why not use a lazy-loading getter?
The method is private, that makes this method a dead-end in a scenario where, for example, the content-type needs to be changed
If you have access to the HttpPost class, why not add a method setEndPoint, so you don't need to create a new instance time and time again


On the whole, I'd start with changing the access modifier to protected, so that in a child class, you can do something like:

protected function createHttpPost($endPoint, array $options = null)
{
    if ($options === null)//default, use parent version
        return parent::createHttpPost($endPoint);
    $httpPost = new HttpPost($this-&gt;baseUri.'/'.$endPoint);
    $httpPost-&gt;setContentType($options['contentType'])
        -&gt;setAccept($options['accept']);
    //and so on...
}


If you can alter the HttpPost class, I'd also encourage you to add methods that reset the instance to a pristine state (a state that allows another request). Then you can lazy-load this instance as a property, and reuse it:

protected function createHttpPost($endPoint, array $options)
{
    $httpPost = $this-&gt;getHttpPost();//see below
    $httpPost-&gt;setContentType($options['contentType'])
        -&gt;setAccept($options['accept'])
        -&gt;setEndPoint($this-&gt;baseUri.'/'.$endPoint);
    //and so on
}
/**
 * Lazy-loader for HttpPost instance
 * @return HttpPost
 */
protected function getHttpPost()
{
    if ($this-&gt;httpPost === null)
        $this-&gt;httpPost = new HttpPost;
    else
        $this-&gt;httpPost-&gt;resetInstance();
    return $this-&gt;httpPost;
}


This way, the constructor will only be called once, and only when the instance of HttpPost is required. Then, you can re-use the instance for each request.
If the instance of HttpPost is passed around, or a request is not made instantaneously, then there could be a risk of this request object losing its state, before a request is made. If this is possible, then of course, you don't use a lazy-loader like the one above, but just use a getter function, or keep doing what you do (ie: create an instance ad-hoc).