在PHP中扩展方法

I have two methods which are 90% identical, ie 90% repetitive code. I am trying to extend the 2nd method.

First Method:

public function getResultsByID($userID = null){
    $sqlParams = array();
    if (!$userID)
            {
                throw new Exception("No User ID Provided");
            }   

    $sqlParams['userID'] = $userID;
    $sql = "SELECT t.user_id,
                   t.owner_id,
                   t.store_id
            FROM users t
            LEFT JOIN store s
            ON s.store_id = t.store_id
            WHERE t.user_id = :userID";

    $db = $this->dbWrite : $this->dbRead;       
    $results = $db->getRow($sql, $sqlParams);

    return $results;

}

My Second method is pretty identical except that I am joining multiple tables into this.

public function getMoreResultsByID($userID = null){
    $sqlParams = array();
    if (!$userID)
            {
                throw new Exception("No User ID Provided");
            }   

    $sqlParams['userID'] = $userID;
    $sql = "SELECT t.user_id,
                   t.owner_id,
                   t.store_id
            FROM users t
            LEFT JOIN store s ON s.store_id = t.store_id
            LEFT JOIN owner_contact oc ON oc.owner_id = t.owner_id
            LEFT JOIN owner_detail od ON od.owner_id = t.owner_id
            WHERE t.user_id = :userID";

    $db = $this->dbWrite : $this->dbRead;       
    $results = $db->getRow($sql, $sqlParams);

    return $results;

}

I am unable to figure out how to extend my getMoreResultsByID() from getResultsByID() so that I can get rid of identical code

Thanks in advance

You dont need to add two function to do same work all you need to do is pass parameter to function

public function getResultsByID($userID = null,$flag){
    $sqlParams = array();
    if (!$userID)
        {
            throw new Exception("No User ID Provided");
        }   

    $sqlParams['userID'] = $userID;
    if($flag=value1){
       $sql = "SELECT t.user_id,t.owner_id,t.store_id
        FROM users t
        LEFT JOIN store s
        ON s.store_id = t.store_id
        WHERE t.user_id = :userID";
    }elseif($flag=value2){
        $sql = "SELECT t.user_id,
               t.owner_id,
               t.store_id
        FROM users t
        LEFT JOIN store s ON s.store_id = t.store_id
        LEFT JOIN owner_contact oc ON oc.owner_id = t.owner_id
        LEFT JOIN owner_detail od ON od.owner_id = t.owner_id
        WHERE t.user_id = :userID";
    }

    $db = $this->dbWrite : $this->dbRead;       
    $results = $db->getRow($sql, $sqlParams);

    return $results;
} 

You could create another private method where you pass SQL query and userId as parameters like this.

<?php

private function queryResults($sql, $userID = null){
    $sqlParams = array();
     if (!$userID)
        {
            throw new Exception("No User ID Provided");
        }   

    $sqlParams['userID'] = $userID;
    $db = $this->dbWrite : $this->dbRead;       
    $results = $db->getRow($sql, $sqlParams);

    return $results;

}
?>

Then, you can use your methods like this.

<?php
public function getResultsByID($userID = null){
    $sql = "SELECT t.user_id,
                   t.owner_id,
                   t.store_id
            FROM users t
            LEFT JOIN store s
            ON s.store_id = t.store_id
            WHERE t.user_id = :userID";

    return $this->queryResults($sql, $userID);
}

Similarly, the other one. You can also modify your queryResults() method further, to serve other methods in your class.

You can pass two parameters two the function getResultsById

<?php

public function getMoreResultsByID($userID = null, join = TRUE){
    $sqlParams = array();
    if (!$userID)
            {
                throw new Exception("No User ID Provided");
            }   

    $sqlParams['userID'] = $userID;
    $sql = "SELECT t.user_id,
                   t.owner_id,
                   t.store_id
            FROM users t";
    if (join){
        $sql.="LEFT JOIN store s ON s.store_id = t.store_id
                LEFT JOIN owner_contact oc ON oc.owner_id = t.owner_id
                LEFT JOIN owner_detail od ON od.owner_id = t.owner_id";
    }else{
        $sql.="LEFT JOIN store s
                ON s.store_id = t.store_id";
    }
    $sql.="WHERE t.user_id = :userID";

    $db = $this->dbWrite : $this->dbRead;       
    $results = $db->getRow($sql, $sqlParams);

    return $results;

}