Pulling people out of a database I have this class that pulls people out of a database. I want it to be a little prettier and function better.

&lt;?php
class comps extends db_config {

    public $output = array();
    private $arr = array();
    public $area;
    public $me;
    public $options = array();

    public function __construct() {
        $this-&gt;pdo = new PDO('mysql:host='.$this-&gt;_host.';dbname='.$this-&gt;_db.';charset='.$this-&gt;_charset, $this-&gt;_user, $this-&gt;_pass);
    }

    public function __toString() {
        return var_dump($this-&gt;result());
    }

    public function result() {
        return $this-&gt;output;
    }

    public function setoption($option, $value) {
        array_push($this-&gt;options, array($option=&gt;$value));
    }

    public function run() {

        $where = array();
        $limit = '';
        $showMe = '';

        if (isset($this-&gt;options['admin'])) {
            $where[] = "admin = '".$this-&gt;options['admin']."'";
        }

        if (isset($this-&gt;options['area'])) {
            $where[] = "area = '".$this-&gt;area."'";
        }

        if (isset($this-&gt;options['showMe']) &amp;&amp; $this-&gt;options['showMe'] = true) {
            $showMe = "AND missionary &lt;&gt; '".$this-&gt;me."'";
        }

        if (isset($this-&gt;options['limit'])) {
            $limit = " LIMIT ".$this-&gt;options['admin'];
        }

        if (count($where) &gt; 0) {

            $where = implode(' AND ', $where);

        } else {
            $where = '';
        }

        $area = isset($this-&gt;area) ? $this-&gt;area : '';

        if ($area != '') {
            $sth = $this-&gt;pdo-&gt;prepare("SELECT missionary FROM missionary_areas WHERE area_uid = :area AND missionary_released = '0' ".$showMe);
            $sth-&gt;bindParam(':area', $area);
            $sth-&gt;execute();
            $sth-&gt;setFetchMode(PDO::FETCH_OBJ);

            while ($mis = $sth-&gt;fetch()) {
                $sth2 = $this-&gt;pdo-&gt;prepare("SELECT * FROM missionarios WHERE ".$where." mid = :mid LIMIT 1");
                $sth2-&gt;bindParam(':mid', $mis-&gt;missionary);
                $sth2-&gt;execute();

                $this-&gt;arr[] = $sth2-&gt;fetch(PDO::FETCH_ASSOC);
            }

            foreach($this-&gt;arr as $a) {
                array_push($this-&gt;output, $a);
            }
        } else {
            $sth = $this-&gt;pdo-&gt;prepare("SELECT * FROM missionarios ".$where.$limit);
            $sth-&gt;execute();
            $res = $sth-&gt;fetchAll(PDO::FETCH_ASSOC);

            foreach($res as $a) {
                array_push($this-&gt;output, $a);
            }
        }

    }
}
Here's the code smells:


The new keyword in a class definition
A result() method and $output attribute for data created by single method
A very long, ambiguous method: run
Two mostly-different SQL statements separated by an if-else


And here's what I'd do to eliminate them:


Move the PDO object to a constructor argument (see below) so you're not violating the Law of Demeter (see #1 and 2 here)
Remove result() altogether and simply return the result from the method in which it's built.
Break this into smaller, more meaningful methods based on responsibility (see #4 below)
Create getMissionaryByArea() and getAllMissionaries() methods with optional parameters


Here's what my code might look like:

&lt;?php

/**
 * This class name is ambiguous.
 * I'm not sure what it means, so I cannot suggest a better name.
 */
class comps { 

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

    public function getMissionariesByArea($area, $admin_id=false, $omit_missionary_id=false, $limit=100) {
        $sql = &lt;&lt;&lt;'ENDSQL'
        SELECT m.*
        FROM missionarios m
        JOIN missionary_areas ma ON ma.missionary = m.mid
        WHERE ma.area_uid = :area 
        AND ma.missionary_released = '0'
        LIMIT :limit
ENDSQL;
        $params = [
            'area' =&gt; $area,
            'limit' =&gt; $limit
        ];

        if ($omit_missionary_id) { 
            $sql .= ' AND ma.missionary &lt;&gt; :omit_missionary_id';
            $params['omit_missionary_id'] = $omit_missionary_id;
        }

        if ($admin_id) {
            $sql .= ' AND m.admin = :admin_id';
            $params['admin_id'] = $admin_id;
        }

        $sth = $this-&gt;pdo-&gt;prepare($sql);
        $sth-&gt;execute($params);
        return $sth-&gt;fetchAll();
    }

    public function getAllMissionaries($admin_id=false, $limit=100) {
        $sql = 'SELECT * FROM missionarios';
        $params = ['limit' =&gt; $limit];

        if ($admin_id) {
            $sql .= ' AND admin = :admin_id';
            $params['admin_id'] = $admin_id;
        }

        $sth = $this-&gt;pdo-&gt;prepare($sql);
        $sth-&gt;execute($params);
        return $sth-&gt;fetchAll();
    }
}


I can revise if I've missed something in my refactoring.