1

In my demo project I want to access static variable of some class, but those variable name are passing dynamically. am trying to use function variable like given below:

public function filterBy($params)
    {
        foreach ($params as $key=>$value) {
            $filter_field_name = strtoupper($key);
            $this->criteria->add(ProductPeer::$filter_field_name, $value, Criteria::EQUAL);
        }
        return $this;
    }

It gives me error

Fatal error: Access to undeclared static property: ProductPeer::$filter_field_name in /home/sfprojects/shopme/lib/product/ProductDb.php on line 47

Though if I use ProductPeer::STATUS instead of ProductPeer::$filter_field_name then it works.

What's wrong here?

2 Answers 2

1

There is a function constant() that can do this:

constant('ProductPeer::' . $filter_field_name);
Sign up to request clarification or add additional context in comments.

2 Comments

Yup, works perfect! thanks. though I don't know reason behind it, if you can tell reason behind access via constant function then it would be helpful
I suppose that the $ accesses only properties of the class, whereas constants are a separate language construct which works different.
0

$this->criteria->add(ProductPeer::$$filter_field_name, $value, Criteria::EQUAL);

Use double dollar sign for reference of other variable. Like $$filter_field_name

Comments

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.