Reduce similar methods via pattern? I've put together a simple application in order for me to learn sqlite and OOP in PHP. The application basically outputs two random words, e.g. "Angry Badger", from two database tables, one called Animals &amp; the other named Adjectives. At present both tables contain the same columns (but could change in future).

The code uses an interface, iWord with two methods to be implemented by their concrete classes (Animal, Adjective). The database object has been passed in on the constructors so I can create some unit tests later. 

If I were to introduce a new class, fruit, with the same methods: getRandomItem() &amp; listRandomItems($numerOfItems) I would need to create the another DB table, class, implement the same DB error handling and processing of data. This doesn't feel correct.

An alternative may be to combine the database tables into one and have one class that I pass in an item to lookup, e.g. animal, fruit, adjective, &amp; dynamically build the SQL. 

Any recommendations or patterns that I should be following?

Please note: this is test code for learning &amp; not for production use ;) 

&lt;?php

interface iWord
{
    public function getRandomItem();
    public function listRandomItems($numerOfItems);
}

class Animal implements iWord
{
    private $database;

    public function _construct(PDO $pdo)
    {
        $this-&gt;database = $pdo;
    }

    public function getRandomItem() 
    {
        $rAnimal = NULL;
        try 
        {
            $query = $this-&gt;database-&gt;prepare("SELECT animalName FROM animal ORDER BY RANDOM() LIMIT 1;");
            $query-&gt;execute();
            $sqlres1 = $query-&gt;fetch(PDO::FETCH_OBJ);
            $rAnimal = $sqlres1-&gt;animalName;
        }
        catch (PDOException $ex)
        {
            echo $ex-&gt;getMessage();
        }
        return $rAnimal;    
    }

    public function listRandomItems($numerOfItems)
    {
        // not yet implemented
        throw new Exception('Not Implemented');
    }
}

class Adjective implements iWord
{
    private $database;

    public function _construct(PDO $pdo)
    {
        $this-&gt;database = $pdo;
    }

    public function getRandomItem() 
    {    
         // exception handling required
         $query = $this-&gt;database-&gt;prepare("SELECT adjective FROM adjectives ORDER BY RANDOM() LIMIT 1;");
         $query-&gt;execute();
         $sqlres2 = $query-&gt;fetch(PDO::FETCH_OBJ);
         return $sqlres2-&gt;adjective;
     }

    public function listRandomItems($numerOfItems)
    {
        throw new Exception('Not Implemented');
    }    
}

try {
    $pdo = new PDO("sqlite:c:/htdocs/mydb.sqlite");
    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
    echo $e-&gt;getMessage();
}

$adj = new Adjective($pdo);
$rAdjective = $adj-&gt;getRandomItem();
$anm = new Animal($pdo);
$rAnimal = $anm-&gt;getRandomItem();

echo $rAdjective . " " . $rAnimal;

unset($pdo);
Extract an abstract base class AbstractWordTable that implements the better-named interface WordTable by moving all code that's duplicated between Adjective and Animal (all but the table and column names). This is a major step in the DRY direction that will simplify your code.

I won't attempt to produce this code on my phone, but the key is for those concrete implementations to contain only unique code. The end result will be with a simple one-line constructor.

Another improvement would be to decouple reading the table contents from picking random words by splitting these functions between WordTable that reads words from a database table into an array and WordList that provides various ways to consume it, including choosing random words and subsets.