inspireddesign Posted August 9, 2009 Share Posted August 9, 2009 Hello all, I have the following code that I can't seem to pass the var to the method inside it's class. Can someone help. It says it's missing an argument. My problem: I'm trying to echo the value entered when I call the class: So in this case I'm trying to echo '40' and when I get that to work, it will be the id used to fetch the data from the db. Thanks! <? $openclose = new showHide(); $openclose->showHide(40); ?> Here is the class: <? class showHide { private $id; private $returnResults; public function showHide($id){ global $contacts; // Bring in MySQL DB Link echo $this->id; die; $query = "SELECT * FROM contacts WHERE contact_id = $this->id"; $result = mysql_query($query, $contacts) or die("ERROR: Unable to fetch field value: \n" . mysql_error()); $this->returnResults = mysql_fetch_assoc($result); echo $this->returnResults['contact_primary'] . ' - ' . $this->returnResults['contact_id']. '<br />' ; } } ?> Link to comment https://forums.phpfreaks.com/topic/169421-solved-easy-one-i-hope/ Share on other sites More sharing options...
MadTechie Posted August 9, 2009 Share Posted August 9, 2009 okay your trying to echo $this->id but you havn't set it, $id has been passed but its not set to $this->id The reason for the missing an argument is because your not setting it.. when a function name has the same name as the class the function is called during the construction stage so $openclose = new showHide(); is calling the function and you can see your not setting a var! so a simple fix would be <?php $openclose = new showHide(40); //construction echo "<br>".$openclose->WhatsTheID(); //another function class showHide { private $id; private $returnResults; public function showHide($id){ //construction //pass the var global $contacts; // Bring in MySQL DB Link //we will use this in other places so we will set it to the private id $this->id = $id; //set private var to passed one echo $this->id; return; $query = "SELECT * FROM contacts WHERE contact_id = $this->id"; $result = mysql_query($query, $contacts) or die("ERROR: Unable to fetch field value: \n" . mysql_error()); $this->returnResults = mysql_fetch_assoc($result); echo $this->returnResults['contact_primary'] . ' - ' . $this->returnResults['contact_id']. '<br />' ; } function WhatsTheID() { return $this->id; } } ?> Link to comment https://forums.phpfreaks.com/topic/169421-solved-easy-one-i-hope/#findComment-893879 Share on other sites More sharing options...
inspireddesign Posted August 9, 2009 Author Share Posted August 9, 2009 Thanks a bunch! I understand. Much different than I'm used to. Link to comment https://forums.phpfreaks.com/topic/169421-solved-easy-one-i-hope/#findComment-893888 Share on other sites More sharing options...
MadTechie Posted August 9, 2009 Share Posted August 9, 2009 Your welcome Your get used to it and prefer them after a while just remember that $this->id = $id is only needed if you intend to use the ID later in the class without passing it again Link to comment https://forums.phpfreaks.com/topic/169421-solved-easy-one-i-hope/#findComment-893893 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.