Hi, I have a class Customer with a bunch of protected variables like $_name, $_address, etc. If I wanna read the variables I could write a function for every protected variable like
Code: Select all
public function getAddress() {return $this->_address}
etc. Since the number of protected variables is getting rather large in my program, and I don't want to make them public, I would like to make one function that would show return the variable ... something like:
Code: Select all
class Customer {
protected $_name;
protected $_address;
public function getVar($str) {
// some code to return variable with name $this->_$str
}
}
$customer = new Customer("John Doe, Main Street 1, ...);
echo $customer->getVar("address");
What code would I need in the getVar function to return the value of $_address?