Jump to content

Passing Values To Variables In A Php Class


sasa1985

Recommended Posts

i need to pass the values form a form to the class so that i can use them inside the class . this was the code i have tried out, which didn't obviously work<?phpclass Example { /////////////////////variables//////////////$usr = $_POST['user'];$pas = $_POST['pass']; var $user ; var $pass ; ////////////setters & getters ////////////////// function set_user($usr) { $this-> user= $usr; } function get_user() { return $this-> user; } function set_pass($pas) { $this-> pass= $pas; } function get_pass() { return $this-> pass; } ///////////////////////////////////////////////// function foo() { echo "foo!\n"; } }// create an Example object$e = new Example();echo $e->get_user(); echo $e->foo();echo $e->get_pass();?>i can assign values for variables ($user,$pass) instead they taking from outside the class. But it doesn't serve my purpose. what i really need is to get value from outside to assign to variables.

Link to comment
Share on other sites

How about using a constructor?

class Example {/////////////////////variables//////////////$user, $pass;__construct($user = $_POST['user'], $pass = $_POST['pass']) {$this->user = $user;$this->pass = $pass;}

If that doesn't work, something along those lines should.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...