ZachMEdwards Posted May 28, 2010 Share Posted May 28, 2010 I'm new to PHP classes. Can someone show me why this is throwing a notice / fatal error? And how do I fix it? test.php: <?PHP class testVariable { private $test = 'testing'; public function __construct() { echo $this->$test; } } ?> index.php: <?PHP require_once 'test.php'; $test = new testVariable(); ?> Notice: Undefined variable: test in C:\www\test.php on line 6 Fatal error: Cannot access empty property in C:\www\test.php on line 6 Link to comment https://forums.phpfreaks.com/topic/203235-undefined-variable-notice-in-class/ Share on other sites More sharing options...
Mchl Posted May 28, 2010 Share Posted May 28, 2010 echo $this->test; BTW: It's customary to start class names with Capital letter. class TestVariable { Link to comment https://forums.phpfreaks.com/topic/203235-undefined-variable-notice-in-class/#findComment-1064832 Share on other sites More sharing options...
ZachMEdwards Posted May 28, 2010 Author Share Posted May 28, 2010 Thank you, and I'll remember the capitalization custom. Link to comment https://forums.phpfreaks.com/topic/203235-undefined-variable-notice-in-class/#findComment-1064834 Share on other sites More sharing options...
The Eagle Posted May 28, 2010 Share Posted May 28, 2010 Try with the spacing. Also $this (I thought it was a pre-defined variable) isn't defined but I am not too sure if that's the issue. echo " .$this->$test. "; Link to comment https://forums.phpfreaks.com/topic/203235-undefined-variable-notice-in-class/#findComment-1064835 Share on other sites More sharing options...
Mchl Posted May 28, 2010 Share Posted May 28, 2010 Eagle: $this is a variable containing current object. It needs not to be defined. See: http://php.net/manual/en/language.oop5.php Link to comment https://forums.phpfreaks.com/topic/203235-undefined-variable-notice-in-class/#findComment-1064841 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.