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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:10:56+00:00 2026-06-10T18:10:56+00:00

Recently, I wrote a simple web application. I currently have. A static page which

  • 0

Recently, I wrote a simple web application.
I currently have.

  • A static page which serves the html markup and the resources I need
  • A javascript routing handling mechanism which communicate with the server and render responses on the client.

For each object I need to manipulate, the server provides a php script at /app/object.php which accepts POST data and return JSON results.

Eg (not actual responses):

POST /app/comments.php?a=add&page_id=43&author=A&text=Some%20Text
{"s":"OK"}

POST /app/comments.php?a=list&page_id=43
[{ "author": 'A', "text": "Some text"}, { "author": "B", "text": "Other text"}]

POST /app/users.php?a=list
["A", "B", "C"]

Under the hood the JSON api is implemented like that:

//comments.php
require('init.php'); 
// start sessions, open database connections with data in config.php

switch(POST('a')){
case "list":
    //.... retrieving data
    echo json_encode($data)
break;
case "add":
    //.... inserting data
    echo '{"s":"OK"}';
break;
}

The largest object has 7 methods and 200 (well-indented, not compressed) LOC, while the average is roughly 3 methods per object.

A developer friend of mine is suggesting to replace the switch with objects, to make it “simpler”, “more extensible” and “more maintainable”.

I frankly don’t see how a system like that could get any simpler (especially by using objects) but I’d love to know other developers’ opinions.

Ignoring the performance hit of using objects in PHP, should I use a class-based approach?

If yes, how can I structure my JSON API in order to use objects, without adding too much code (and thus decreasing the maintainability of the project)?

  • 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-10T18:10:58+00:00Added an answer on June 10, 2026 at 6:10 pm
    <?php
    
    class Controller {
      protected $post;
      public function __construct() {
         $this->post = $post;
      }
      public function __call($name, $arguments) {
        if(!method_exists($this, $name)) {
          die("Unknown action!");
        }
      }
      public function whatever() {
        echo json_encode($this->post['page_id']);
      }
      public function data() {
        echo '{"s":"OK"}';
      }
      // new action? just add another method
    }
    
    
    $controller = new Controller();
    $controller->{$_POST('a')}(); // example 1
    
    $controller->data(); // you can't do it using switch
    
    1. Easy to add new methods
    2. Easy to maintance
    3. You can fire your methods whenever your want
    4. Code is tidy
    5. It’s really common practice
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

recently I wrote a basic web application with only index.jsp and a simple servlet,
I have been looking at Reflection.Emit recently. I wrote a simple program that generates
I recently bought a Kindle 3G which comes with a simple Webkit-based web browser.
I recently wrote a program that used a simple producer/consumer pattern. It initially had
I recently wrote a script which queries PyPI and downloads a package; however, the
I recently wrote, my first, WPF application that has a list of items that
I recently wrote a simple bootloader in assembly for floppy disk booting. I used
Because I have a poor memory, I want to write a simple application to
I have recently begun a new project which runs on remote computer. I usually
I have a simple SQLite database I use to track invoices. I recently decided

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.