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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:22:40+00:00 2026-05-28T03:22:40+00:00

I’m trying to implement what I believe is a factory class. I have an

  • 0

I’m trying to implement what I believe is a factory class. I have an API framework. After my front controller has processed the request it attempts to return the output to the client. The call in my front controller looks like this:

<?php
...
$response_obj = new Response($response_str, 'json');
echo $response_obj->render();
?>

My Response class basically takes the second argument as the type of class to instantiate, and passes this new class the contents of $response_str. Here it is:

<?php
class Response {

    public function __construct($data, $format) {
        switch ($format) {
            case 'json':
                $obj = new ResponseJson($data);
            break;
        }
        return $obj;
    }
}

And then my ResponseJson class looks as follows:

<?php
class ResponseJson {

    protected $data;

    public function __construct($data) {
        $this->data = $data;
        return $this;
    }

    public function render() {
        header('Content-Type: application/json');
        return json_encode($this->data);
    }
}

However, in my front controller $response_obj returns its type as Response and not ResponseJson as would be expected, and a call to the render() method (which exists in ResponseJson and not Response) throws a Fatal error:

Fatal error: Call to undefined method Response::render() in /Users/Martin/Sites/api-framework/index.php on line 61

Where am I going wrong?

  • 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-28T03:22:41+00:00Added an answer on May 28, 2026 at 3:22 am

    what you describe is not a factory, but rather invalid code. (the __construct will always return a new instance of the class it is implemented in. in fact, it does not need to return anything because this has no effect!)

    A factory is usually implemented as a static function that can be called without instantiating an object of the factory class. It then creates an object instance of a class (in most cases of another class, as in your case) and returns that.

    your Response class should look like this:

    <?php
    class Response {
    
        static public function create($data, $format) {
            switch ($format) {
                case 'json':
                    $obj = new ResponseJson($data);
                    break;
                case default:
                    return NULL; // or throw exception!
                    break;
            }
            return $obj;
        }
    }
    

    and your frontend controller like this:

    <?php
    ...
    $response_obj = Response::create($response_str, 'json');
    echo $response_obj->render();
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.