1

I am new to OOP and have written a products class. All is working fine but I am unsure which of the below version of a method within this class is best?

The first gets the variables from within the object and the second passes the variables into the class. Both work. I originally had it as the first version but things seems to be running slow and then changed it to the second.

 public function getProductURLstart(){  

    $select = "SELECT l.URL, p.id FROM logins AS l
    INNER JOIN Pages AS p ON l.id = p.clientID 
    WHERE l.id = '$this->skID' AND p.productPage = 1";

    $res = mssql_query($select);
    $r = mssql_fetch_row($res);     

    $url = trim($r[0]); 
    $page_id = $r[1];

    return  $url .'/index.aspx?pageID='. $page_id . '&prodID=$this->prodID';

}

OR

 static function getProductURLstart($skID, $prodId){    

    $select = "SELECT l.URL, p.id FROM logins AS l
    INNER JOIN Pages AS p ON l.id = p.clientID 
    WHERE l.id = '$skID' AND p.productPage = 1";

    $res = mssql_query($select);
    $r = mssql_fetch_row($res);     

    $url = trim($r[0]); 
    $page_id = $r[1];

    return  $url .'/index.aspx?pageID='. $page_id . '&prodID=$prodId';

}
2
  • 2
    You could post this in Code Review to get better answers Commented Nov 29, 2011 at 14:58
  • 1
    Are you using this in a loop. Which would be bad given the fact that you make a call to the DB for a just the page ID. Hopefully you store these in a cache, to prevent excessive calls to the DB for the same data. Commented Nov 29, 2011 at 15:24

3 Answers 3

1

If the instance of this class is for a single product, then use the first method,as there would be no reason to pass it in as parameters if you have them set when you construct the class.

Otherwise, if this is for more than on product, then the second method would be your best choice. As you will not have to call and set methods for the skID and prodID every time you need to get a product URL.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I will have to call this multiple times, I first call the product class to get a list of all the products then loop through each product and call this method.
Can you get the page ID when you get the list of products? Otherwise you may want to beef up your 'getProductURLstart()' method to take a list of products and return results so that the product id is paired with the page ID in each resulting row. I personally would use a stored procedure for this particular part, instead of a simple quick select like your doing now. If you write it efficiently, bet things would speed up dramatically.
0

It depends, if you plan to give models some functionality like making them a little bit like active records, you can put the functionality in the class and use the class' members. Besides, do you have a good reason to use static functions? If you want to apply OOP, you have to give responsibilities to meaningful classes, a model should not both get data and do the redirection.

Comments

0

I'll be with the first. I always develop my app trying to use the less static methods I can and always using attributes, avoiding sending them by function parameters.

1 Comment

yeah - I thought that too, but as b01 mentions, I am calling this method multiple times in a loop and would therefore have to set $productObj->$prodId and $productObj->$skId every time I loop through each product.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.