Rupal Rupal - 6 months ago 10
PHP Question

Error in access function variable as static variable

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?

Answer

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

constant('ProductPeer::' . $filter_field_name);