Link Posted June 13, 2007 Share Posted June 13, 2007 I am having a problem with the code below. I create an instance of the class 'user' in another script, passing a string. The class successfully calls function 'user1' and then proceeds to do as it says. But, then going back to the other script it says that the variable 'table' is not set. I debugged by testing to see if it would print out, and it prints out in the function 'user1', but not from any global perspective. As strange as that is, if I then use the 'setTable', and then proceed to call 'getTable' in the other script, it works just fine. Why doesn't the constructor approach work while the 'setTable' does? See code below: <?php class user { var $table; var $userid; function user() { global $table; session_start(); $args_count = func_num_args(); if ($args_count > 0) { $name = "user".$args_count; $args = func_get_args(); call_user_func_array(array(get_class($this), $name), $args); } } function user1($table_name) { $this->table = $table_name; } function user2($table_name, $id) { $this->table = $table_name; $this->userid = $id; } function setTable($table_name) { $this->table = $table_name; } function getTable() { return $this->table; } function setUserid($id) { $this->userid = $id; } function getUserid() { return $this->userid; } function checkLogin() { global $_SESSION; if ($_SESSION['userid']) { $this->userid = $_SESSION['userid']; return true; } else return false; } function isUser($username) { $database = new database; $result = mysql_query("SELECT * FROM ".$this->table." WHERE username=".$database->quote($username)); if (mysql_num_rows($result) == 0) return false; else return true; } function login($username, $password) { global $_SESSION; $database = new database; $result = mysql_query("SELECT * FROM ".$this->table." WHERE username=".$database->quote($username)." AND password='".md5($password)."'"); if (mysql_num_rows($result) != 1) return false; else { $info = mysql_fetch_array($result); $_SESSION['userid'] = $info['id']; $this->userid = $info['id']; $_SESSION['username'] = $info['username']; return $info; } } function getUserarray() { $args_count = func_num_args(); if ($args_count > 0) { $name = "getUserarray".$args_count; $args = func_get_args(); call_user_func_array(array(get_class($this), $name), $args); } } function getUserarray1($id) { $database = new database; $this->userid = $id; $result = mysql_query("SELECT * FROM ".$this->table." WHERE id='$id'"); return mysql_fetch_array($result); } function getUserarray2($username, $password) { $database = new database; $result = mysql_query("SELECT * FROM ".$this->table." WHERE username=".$database->quote($username)." AND password='".md5($password)."'"); $info = mysql_fetch_array($result); $this->userid = $info['id']; return $info; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/55387-scope-problem-i-think/ Share on other sites More sharing options...
Link Posted June 13, 2007 Author Share Posted June 13, 2007 Anyone? Anything? Quote Link to comment https://forums.phpfreaks.com/topic/55387-scope-problem-i-think/#findComment-274116 Share on other sites More sharing options...
Link Posted June 14, 2007 Author Share Posted June 14, 2007 The values are passed through a constructor. The code in the constructor is to call a function based on the number of arguments. You can remove the "global $table;" line as it has no bearing on the issue. The script uses the constructor to, in this case, call "user1()" and set the table name. So, locally, in the function user1(), it sets it, but then it doesn't outside of the function. What puzzles me is that I can call setTable() after creating the instance of the class, and it works just as it should, updating the class variable $table. So, the issue may very well be scope, but my problem is why user1() doesn't work but setTable() does when it's the exact same code. So the following doesn't do anything: <?php require "../include/include_all.inc.php"; $user = new user("test1"); print "Table: ".$user->getTable()."<br><br>"; ?> It just prints out: Table: But then, on top of that, after creating the instance of the class, I can call user1 again, even though I know it was called by the constructor, and it works just as setTable() does: <?php require "../include/include_all.inc.php"; $user = new user("test1"); $user->user1("test2"); print "Table: ".$user->getTable()."<br><br>"; ?> Which outputs: Table: test2 So, I guess the question in the end is why can't it set the class variable $table when the constructor calls user1() even though it can do it outside of the constructor? Quote Link to comment https://forums.phpfreaks.com/topic/55387-scope-problem-i-think/#findComment-274329 Share on other sites More sharing options...
KrisNz Posted June 19, 2007 Share Posted June 19, 2007 I put your class in one file and then included it another and it worked fine. you could try replacing //this call_user_func_array(array(get_class($this), $name), $args); ///with... call_user_func_array(array($this, $name), $args); so that your not calling a non static method as though it were. I don't think thats the problem though. Quote Link to comment https://forums.phpfreaks.com/topic/55387-scope-problem-i-think/#findComment-277472 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.