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

  • Home
  • SEARCH
  • 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 6583575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:25:37+00:00 2026-05-25T16:25:37+00:00

Examples I read of class structure typically begin with a base class and that

  • 0

Examples I read of class structure typically begin with a base class and that base class gets extended with more refined classes ie. the often quoted:

class Animal {}
class Rodent extends Animal {}
class Mouse extends Rodent {}

But in my real world project of a CMS/ecommerce system I seem to be building this structure the other way around i.e. starting with a class for one type of situtation and extending it with something related to the whole project but not actually to the extending class.

class page {}
class product extends page{}
class category extends product{}
class basket extends category{}
class shop extends basket{}

So this way I am just calling $s = new shop() and getting access to all the classes/methods needed to run the shop. I suppose I am just extending the classes to save instantiating each one separately. This seems backwards to most examples I have read and I’m surely missing what OOP is all about here. My classes seem to be getting more general and less specialised as they get extended.

Should I continue in this way or restructure how I am building this class system?

  • 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-25T16:25:38+00:00Added an answer on May 25, 2026 at 4:25 pm

    You could google composition over inheritance and is-a vs has-a.

    In your design sketch, you mix up a bunch of stuff as you noticed. The easiest example to dissect would be class category{} extends product. One category is not a product, but it has/contains several products. This would be more suitable:

    class product {
            public $title; // for simplicity
            public $price; // for simplicity
    }
    class category {
            private $name;
            private $products;
            public function __construct($name) {
                    $this->name = $name;
                    $this->products = array();
            }   
            public function addProduct(product $p) {
                    $this->products[] = $p; 
            }   
            public function printProducts() {
                    $product_names = array_map(function($product) {
                            return $product->title;
                    }, $this->products);
                    echo implode(', ', $product_names);
            }   
    }
    
    $c = new category('fruits');
    $apple = new product;
    $apple->title = 'apple';
    $orange = new product;
    $orange->title = 'orange';
    $banana = new product;
    $banana->title = 'banana';
    
    $c->addProduct($apple);
    $c->addProduct($orange);
    $c->addProduct($banana);
    
    $c->printProducts();
    

    Notice how you keep both entities separated and let product worry about what a product is and can do, and what a category is (name), has (products) and can do (print a listing of its products).

    Another tip for decoupling (minimizing dependencies between classes) is that you should try to keep data out of your objects for as long as possible. The classes in the example above are simplistic but functional. If you can make them work without database access (which is a common reason to have a huge chain of extends), you realize that none of these classes needs database access and that they instead can be filled/created by classes that do have access to the database. Something like this:

    class category_factory {
      public function __construct(PDO $pdo) { $this->pdo = $pdo; }
      public function getCategories() {
        $rows = $this->pdo->query("SELECT * FROM categories")->fetchAll();
        $categories = array();
        foreach($rows as $row) {
          $categories[] = new category($row['name']);
        }
        return $categories;
      }
    }
    

    *PDO contains a db connection

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Almost all of the examples in the jQuery tutorials that I've read, usually use
I want to read a book that systematically introduces dom with live examples. Any
How can I tell boost that for a particular structure it should not write/read
Am tearing my hair out here. Have read many examples which describe how to
I've read through the Google Spreadsheets API PHP documentation. All examples are using Zend,
Can you recommend me on places where i can read and see examples on
I read that .NET uses connection pooling. For example, if I instantiate a bunch
I've been trying to design my read only Array data structure and I really
For my project I wrote a small config class that loads its data from
I know that you can use java.util.Properties to read Java properties files. See: Java

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.