lawless Posted September 10, 2014 Share Posted September 10, 2014 Hello there After a few years of spending less and less time coding, I've got a lot of catching up to do. Back when I left I usually would run without classes. Now this is a big deal for me today. I do understand the concept of classes and already did some working models, mostly from my learning process. Now here is what is bothering me: <?PHP class database { // Variables public $test; // Constructor public function __construct() { $test = "4"; } // Functions // public function test() { var_dump($this->test); } } $test = new database; $test->test(); ?> Wether I run this script on itself, nor through another file, this does work. What i get is: NULL The constructor does run, I did an echo inside it. Also it does not matter if the variable is public, private or protected - it will be always NULL. Error_reporting is on E_ALL, does not show any errors. What have I overlooked? Link to comment https://forums.phpfreaks.com/topic/290977-this-class-is-killing-me-constructor-runs-does-not-set-values-to-variables/ Share on other sites More sharing options...
Jacques1 Posted September 10, 2014 Share Posted September 10, 2014 $test is just a local variable. It's only available in the constructor itself and thrown away afterwards. If you want to set the test attribute, you need to use $this->test = "4"; Just like in your test() method. Link to comment https://forums.phpfreaks.com/topic/290977-this-class-is-killing-me-constructor-runs-does-not-set-values-to-variables/#findComment-1490618 Share on other sites More sharing options...
lawless Posted September 10, 2014 Author Share Posted September 10, 2014 Awesome, you just made my day I really don't know how I could not see that.. Thank you very much Jacques! Link to comment https://forums.phpfreaks.com/topic/290977-this-class-is-killing-me-constructor-runs-does-not-set-values-to-variables/#findComment-1490619 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.