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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:03:35+00:00 2026-06-15T05:03:35+00:00

I have 5 different model classes. Each one handles different types of data, but

  • 0

I have 5 different model classes. Each one handles different types of data, but they all follow the same format.

I want each kind of model to share functions like toJson, toArray, etc.

There’s a bunch of hack ways I could do this, and I can figure those out, but there HAS TO be a better way.

Here’s my desired effect:

<?
something here{
    function toJson(){return json_encode($this->lastResult);}
}
class applications{
    function findAllFoo(){
        $this->lastResult = $this->db->foo->find();
        return $this;
    }
}

class users{
    function findAllBar(){
        $this->lastResult = $this->db->bar->find();
        return $this;
    }
}
$models = new models();

echo $models->applications->findAllFoo()->toJson();
echo $models->users->findAllBar()->toJson();

I could just do new applications($this) passing the models each an entire set of classes, but I’m sure there’s a way within standard practices to do this.

TL;DR – How do I have a set of classes that shares some helper functions, yet won’t conflict with the __construct function?

  • 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-15T05:03:37+00:00Added an answer on June 15, 2026 at 5:03 am

    If all models share some functionality, why not have a “Model” base class for them?

    abstract class Model {
        protected $lastResult;
        protected $db;
    
        public function __construct(DB $db) {
            $this->db = $db;
        }
    
        public function toJson() {
            return json_encode($this->lastResult);
        }
    }
    
    class Applications extends Model {
        public function findAllFoo() {
            $this->lastResult = $this->db->foo->find();
            return $this;
        }
    }
    
    class ModelBroker {
        public $applications;
    
        public function __construct() {
            $db = /* ... connection code here ... */;
            $this->applications = new Applications($db); // <-- dependency injection
        }
    }
    
    $models = new ModelBroker();
    $json = $models->applications->findAllFoo()->toJson();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have several classes, each of which deals with different sites. They act
I have a group of model peer classes that should all have the same
I have page where I use model which can have different types (depending by
I have a project which exposes object model to use by different types of
I have 3 different types of modal windows to handle different events. They popup
I have a model where multiple classes have a list of value types: class
I currently have two different models: User and Project . The User model has
I am trying to have my client side validation (model binding) to support different
So I have an app with a 2 different models, Comments and Replies, each
I have a page that displays a table in two different modes. In each

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.