gansai Posted July 3, 2018 Share Posted July 3, 2018 How could I pass global variables to set of functions within a class <?php global $user, $scheme_id, $dept_id; class segment { private $user, $scheme_id, $dept_id; public function __construct() { $this->user = $user; $this->scheme = $scheme_id; $this->dept = $dept_id; } public function unitseg_set() { $sql = mysqli->query('SELECT userid, sid, dept_name FROM dept WHERE dept_id = '.$this->dept.' AND userid='.$this->user.''); foreach($sql as $ss) $result = $ss->dept_name; return $result; } } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted July 3, 2018 Share Posted July 3, 2018 (edited) Simple - Don't use globals. Pass them to the object in the constructor class segment { private $user; private $scheme_id; private $dept_id; public function __construct($user, $scheme_id, $dept_id) { $this->user = $user; $this->scheme = $scheme_id; $this->dept = $dept_id; } //etc } $obj = new segment($user, $scheme_id, $dept_id); Edited July 3, 2018 by Barand Quote Link to comment Share on other sites More sharing options...
gansai Posted July 3, 2018 Author Share Posted July 3, 2018 Thank You Barand: How could I call the function from the class segment $seg = new segment($user, $scheme_id, $dept_id); $seg->unitseg_set(); Quote Link to comment Share on other sites More sharing options...
Barand Posted July 3, 2018 Share Posted July 3, 2018 In fact, looking at your previous topic, this is virtually an identical question. Had you considered applying the lesson learned in that one, or did you not learn anything? Quote Link to comment Share on other sites More sharing options...
Barand Posted July 3, 2018 Share Posted July 3, 2018 $result = $seg->unitseg_set(); Your function returns a result set so use that returned value. (BTW, if you haven't noticed, you have made the exact same mistake as in your last topic - mysqli conection is not defined in your class or function.) Quote Link to comment 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.