Jump to content

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
https://forums.phpfreaks.com/topic/307430-passing-global-variables-in-a-class/
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);

 

Edited by Barand

$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.)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.