Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in php class with predefined values?

Tags:

php

class

In my PHP file I have created a class as below but I am getting error on line 3rd and 5th line.

class CommonPath{
    var $baseurl = 'http://mysite.com/';
    var $docroot = realpath(dirname(__FILE__));
    var $root = '/';
    var $images = $this->root.'/img';           
}

My Dreamwaver CS5 showing these lines (3rd & 5th) as erroneous lines and I am getting following error on executing this code.

Parse error: parse error, expecting `','' or `';'' in D:\wamp\www\site\libs\CommonPath.php on line 3
like image 511
djmzfKnm Avatar asked Dec 08 '25 01:12

djmzfKnm


2 Answers

You can have only literals and constants as default values. No functions or other expressions are allowed.

like image 132
linepogl Avatar answered Dec 10 '25 16:12

linepogl


There are two different mistakes. First, you cannot use functions to define class variables (line 3). Moreover, $this does not make sense in line 5, as you have got no object yet.

like image 30
Andrea Avatar answered Dec 10 '25 14:12

Andrea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!