i am getting error in following php code <? class sewatch { public $sum=$this->add(); public function add() { return 5; } } $o=new sewatch(); echo $o->sum; ?> PHP: Parse error: parse error in C:\wamp\www\mainclass.php on line 3 Code (markup):
What are you trying to do exactly? The problem you have is here; public $sum=$this->add(); PHP: Without understanding what you are trying to achieve, exactly, its hard to answer although this; <? class sewatch { public $sum = null; public function __construct() { $this->sum = $this->add(); } public function add() { return 5; } } $o=new sewatch(); echo $o->sum; ?> PHP: Will output "5" if that is all you need.
The 3rd line is not valid, see this documentation page from PHP.net <?php class SimpleClass { // invalid property declarations: public $var1 = 'hello ' . 'world'; public $var2 = <<<EOD hello world EOD; public $var3 = 1+2; public $var4 = self::myStaticMethod(); public $var5 = $myVar; // valid property declarations: public $var6 = myConstant; public $var7 = array(true, false); // This is allowed only in PHP 5.3.0 and later. public $var8 = <<<'EOD' hello world EOD; } ?> PHP: As suggested by lukeg32, you should declare the property then set it's value inside the constructor method __construct()