0

So I'm using a stand alone function from within a class that that uses the class it's being called from. Here's the function

function catalogProductLink($product_id,$product_name,$categories=true) {
    //This is the class that the function is called from
    global $STATE;
if ($categories) {
    //The $STATE->category_id is the property I want to access, which I can't
    if (is_array($STATE->category_id)) {
        foreach($STATE->category_id as $cat_id) {
            if ($cat_id == 0) continue;
            $str .= "c$cat_id/";
        }
    }
}
$str .= catalogUrlKeywords($product_name).'-p'.$product_id.'.html';
return $str;
}

And here's the function call, which is being made from within the $STATE class.

$redirect = catalogProductLink($this->product_id, $tempProd->product_name, true, false);

The object that I need access to is the $STATE object that has been declared global. Prior to this function call there are lots of public properties populated, but when I look at the $STATE object within the function scope it loses all the properties but one, product_id. The property that matters for this function is the category_id property, which is an array of category id's.

I'm wondering why I don't have access to all the public properties of the $STATE object and how I can get access to them.

1
  • Can you expand on "loses all the properties but one"? Are the properties of the object non-existent (i.e., not declared) or are they set to an empty/null value? It sounds like some code not posted here is modifying $STATE before it gets to the catalogProductLink function call. Commented Mar 19, 2010 at 18:14

1 Answer 1

1

What is catalogUrlKeywords() doing? I bet it is modifiying the $STATE This sounds like a horrible design BTW. If global is an integral part of a framework, RUN!

Edit

Or you could just add an optional parameter to take category_id and be done with it. Maybe instead of having $categories=true, make $categories take an array of category_id's or zero.

function catalogProductLink($product_id,$product_name,$category_ids=0) {

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

1 Comment

Yeah I know, it's legacy code, that I'm stuck with. catalogUrlKeywords() just makes a nice looking url from the product name and id, doesn't mess with $STATE at all.

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.