-2

Suppose that I have the PHP code as below:

class modual {
  public $glo;

  static public function SaveData($RDesTechni = 1,$RCapacity = 2){
    try{
      $sql = "INSERT INTO tblworkfaire VALUES('',$RDesTechni,$RCapacity)";
      $qry=mysql_query($sql) or die (mysql_error());
      $id = mysql_insert_id();
      $glo = $id;
    }
    catch(exception $e){
      echo $e;
    }
  }

  static public function ListData(){
    try {
      include("connectdb.php");

      $query = "SELECT * FROM tblworkfaire where id=".$glo;

      return $query;
    }
    catch(exception $e){
      echo $e;
    }
  }
}

What I need:

I want to get the $id to use in the function static public function ListData() but it does not work.

Problems:

It does not work it shows me the error undefined $glo How I fix this? Anyone help me please. Thanks.

Gert Grenander
  • 16,866
  • 6
  • 40
  • 43
user1606816
  • 381
  • 1
  • 3
  • 6

2 Answers2

0

Have you looked into passing the parameter you need to the function?

static public function ListData( $glo ){

When you call the method, you include the value for the id:

$myModual->ListData($id);

In general, it's better design to pass variables to functions rather than use global variables.

You could also make a constructor for the class so that you store a value for $glo when an object of the class is initialized.

Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
0

You have to use self to access the variable, and it also needs to be public static.

class modual {
  public static $glo;

  static public function SaveData($RDesTechni = 1,$RCapacity = 2){
    try{
      self::$glo = 5;
    }
    catch(exception $e){
      echo $e;
    }
  }

  static public function ListData(){
    echo self::$glo;
  }
}
donutdan4114
  • 1,262
  • 1
  • 8
  • 16