unkwntech Posted November 30, 2008 Share Posted November 30, 2008 The following code snip is giving me an error in PHP5: <?php class myClass { public $myVar= $self->myFunction(); //Error here function myFunction() { return 'Hello World'; } } $data = new myClass; echo $data->myVar; ?> The error is "Unexpected T_VARIABLE". What am I doing wrong, and is there anything else I should be aware of? Link to comment https://forums.phpfreaks.com/topic/134846-solved-new-to-oop-need-a-bit-of-help/ Share on other sites More sharing options...
Mchl Posted November 30, 2008 Share Posted November 30, 2008 You can't assign a value to a class variable using methods or funstions. YOu have to move this to __construct() method <?php class myClass { public $myVar //Error here public function __construct() { $this->myVar = $this->myFunction(); } function myFunction() { return 'Hello World'; } } $data = new myClass; echo $data->myVar; ?> Link to comment https://forums.phpfreaks.com/topic/134846-solved-new-to-oop-need-a-bit-of-help/#findComment-702173 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.