Jump to content

Passing global variables in a class


gansai

Recommended Posts

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;
     }
}
?>

 

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.