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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:24:54+00:00 2026-05-24T23:24:54+00:00

Let’s say I want to store dogs in a database table with each dog

  • 0

Let’s say I want to store dogs in a database table with each dog having its own subclass in PHP.

Basically I want to avoid storing/listing the subclass names in different places in the code. What would be a good OOP approach for that?

abstract class Dog {
    protected $data;
    public function __construct($data) {
        $this->data = $data;
    }
    public function name() {
        return $this->data["name"];
    }
    abstract public function breed();
}

class GermanShepherd extends Dog {
    public function breed() {
        return _("German Shepherd");
    }
}

class BullDog extends Dog {
    public function breed() {
        return _("Bulldog");
    }
}

Now I have this class that handles groups of objects (i.e. dogs):

class Dogs {
    public static function getDogs() {
        // ...
        $ret = array();
        while ($row = mysql_fetch_assoc()) {
            switch ($row["type"]) { // I could do this using a lookup array
                 case "shepherd": $dog = "GermanShepherd"; break;
                 case "bulldog": $dog = "Bulldog"; break;
            }
            $ret[] = new $dog($row);
        }
        return $ret;
    }
}

And I would like to use this class to get the dog types in my view (especially for an add dog form), instead of listing the class names:

?><form><select name="type"><?php
foreach (array("GermanShepherd", "Bulldog") as $dog) { // here I would like to do avoid listing the class names again
    ?><option value="<?=$dog ?>"><?php
    $d = new $dog; // actually I can't instantiate the class here because I don't have any data at this point
    echo $d->name();
    ?></option><?php
}
?></select></form><?php

I would like to incorporate this into the Dogs class, something along the lines of this:

class Dogs {
    private static $dogs = array(
        "shepherd" => "GermanShepherd",
        "bulldog" => "Bulldog",
    );
    public static function getDogs() {
        // ...
        $ret = array();
        while ($row = mysql_fetch_assoc()) {
            $dog = self::$dogs[$row["type"]];
            $ret[] = new $dog($row);
        }
        return $ret;
    }

    public static function getDogTypes() {
        return array_values(self::$dogs);
    }
}

?><form><select name="type"><?php
foreach (Dogs::getDogTypes() as $dog) {
    ?><option value="<?=$dog ?>"><?php
    $d = new $dog; // here I still need to instantiate the class and I don't have any data to provide it with
    echo $d->name();
    ?></option><?php
}
?></select></form><?php

This would somewhat work so far, but what if I need more class specific information, for example when I have more fields specific to a dog type?

foreach (Dogs::getDogTypes() as $dog) {
    $d = new $dog; // instantiate again?
    foreach ($d->formFields() as $f) { // I wouldn't do it like this, just putting this here for demonstrative purposes
        echo $f;
    }
}

I think part of the problem lies in the fact that I need to be able to use my classes with and without database data: Everything seems very reasonable when I have the data from the database table, but I also need the data when I generate the form when creating a new dog.

Thanks for your ideas!

  • 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-24T23:24:55+00:00Added an answer on May 24, 2026 at 11:24 pm

    First make use of Interfaces. This will show you that having more specific interfaces (different class methods and properties per subclass) will make you need to deal with them differently in concrete. So they will show you where the deficiencies are and will enable you to streamline your objects into something more re-useable.

    As long as your objects are only storing some data, use a data transfer object instead – which is of any “type”. So you don’t need to deal with type. However, you can use StdClass or Array for that as well if you want to keep it basic. The plus-side is: You don’t need to actually write that much code.

    In case it’s not sufficient (as it will be), only add the code when you need to. Should keep things more simple in the long run then. So start with a basic data transfer object class and build upon it.

    So use the classes you write to separate concerns, not to interweave the concerns. Encapsulate what varies, so your code can actually benefit from your design.

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

Sidebar

Related Questions

Let's say I have table with column 'URL' whrere I store urls like this
Let's say I have a drive such as C:\ , and I want to
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have this MySQL table: OK.. see the type field? Type 0
Let's say R is a mxn random matrix. How can generate R with each
Let's say the Activity I want to start is named OccupyThePieShop I was previously
Let's say we have the following table: CREATE TABLE T ( ID INT, String1
Let's say you create a wizard in an HTML form. One button goes back,
Let's say I'm building a data access layer for an application. Typically I have

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.