3

Is accessing functions while setting class properties possible in PHP (5.2 or 5.3) ?

class DAOClass {

   var $someProperty = SomeObject::staticMethod('readConfigProperty');

}
Smandoli
  • 6,919
  • 3
  • 49
  • 83
matiasf
  • 1,098
  • 1
  • 9
  • 17

2 Answers2

3

It is not possible because you have to initialize the properties with constant values. It isn't even possible to do this:

var $property = array(0);

The way to do what you want to do is inside the class constructor:

class DAOClass {

    var $someProperty;

    public function __construct() {
         $this->someProperty = SomeObject::staticMethod('readConfigProperty');
    }
}

As a side note, using var to declare properties is not the preferred way. Use private, protected or public instead to declare a property along with its visibility (var defaults to public).

Jon
  • 428,835
  • 81
  • 738
  • 806
2

No. In the class declaration you define properties. You do not assign them anything. Everything after the = must be a literal constant. Method/function calls are expressions and cannot be used there.

mario
  • 144,265
  • 20
  • 237
  • 291