ballhogjoni Posted April 22, 2009 Share Posted April 22, 2009 I have variable $a defined as $a=1 in file xyz.php, then in file zyx.php I require_once xyz.php and have a class that call variable $a. Problem is, is that $a shows up as an undefined variable. what gives? xyz.php <?php $a=1; ?> zyx.php <?php require_once xyz.php class whatever { function init(){ echo $a; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155141-solved-variable-scope-help/ Share on other sites More sharing options...
premiso Posted April 22, 2009 Share Posted April 22, 2009 You are using it in a class. It has to be defined by that class. $what = new whatever; $what->serVariable($a); $what->init(); class whatever { private $a; function init(){ echo $this->a; } function setVariable($a) { $this->a = $a; } } Would give you the correct output. If you made $a global in the function init, it will probably work without doing the setVariable part. Quote Link to comment https://forums.phpfreaks.com/topic/155141-solved-variable-scope-help/#findComment-816112 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.