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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:34:25+00:00 2026-05-13T06:34:25+00:00

I have been designing a site locally in PHP 5, but have come across

  • 0

I have been designing a site locally in PHP 5, but have come across a few design issues I’d like advice now.

Currently there are three features of the site and each feature has a class . These features are as follows :

  • a blog
  • a friends list
  • a set of images

I have a class for each but in each class I basically define a similar method that gets all [blogs | Friends | images]. I was wondering if any of you know how I could reduce these classes to be much thinner and probably have one class that is generic between all three features for all methods that are the same for each feature. (i.e getAllById($feature, $id)).

An example function for my existing blog class is as follows:

function getBlogsByUserId($userId) {
    global $db;
    $blogs = array();
    $db->where(array("userId"=>$userId));
    $rows = $db->get("blog")->fetch(0);
    foreach($rows as $row) {
 $blog = new Blog();
 $blog->id = $row['id'];
 $blog->userId = $row['userId'];
 $blog->content = $row['content'];
 $blogs[] = $blog;
    }
return $blogs;
}

Note: I have defined my own class for the DB stuff so don’t worry about that.

I’ve looked at the gateway design pattern but haven’t yet found a solution.
I also want this to be reusable so if I increase the features to seven or more then I won’t have to change much of the class.

Thanks,
Matt

  • 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-13T06:34:25+00:00Added an answer on May 13, 2026 at 6:34 am

    You could create a parent class called, say, Model, like so:

    abstract class Model {
      protected static $_featureTable;
    
      static public function getAllById($id) {
        global $db;
        $items = array();
        $db->where(array("userId"=>$userId));
        $rows = $db->get(self::$_featureTable)->fetch(0);
        foreach($rows as $row) {
          $item = self::getInstance();
          $item->setValues($row);
          $items[] = $item;
        }
        return $items;
      }
    
      abstract static protected function getInstance();
      abstract protected function setValues($row);
    }
    
    class Blog extends Model {
      protected static $_featureTable = 'blogs';
    
      protected static function getInstance() {
        $self = __CLASS__;
        return new $self();
      }
    
      protected function setValues($row) {
        $this->content = $row['content'];
        // etc.
      }
    }
    

    Then, to get a list of blogs:

    $blogs = Blog::getAllById($id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been designing a shopping website lately in PHP. Now in Checkout page
Can anyone help? I have been designing a site using Javascript but the rest
I have been using WCF for a few years now and am fairly comfortable
For the last few days I have been designing a layout for my website
So I have been designing my app in storyboard for a few hours and
I am new comer to android but not to java. I have been designing
Sorry if this is a silly question but lately I've been designing a site
I have been designing with jquery and using the jquery UI widgets .. but
I have been developing on the iPhone with Objective-C for a few months now
I have been working on a SQL Server database designing since long now and

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.