Rupal Rupal - 1 year ago 61
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
instead of
then it works.

What's wrong here?

Answer Source

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

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