snipered Posted June 7, 2007 Share Posted June 7, 2007 Hi, new to this forum and very new to php (2 weeks). I'm trying to teach myself php by following a book. I have a constructor, that's supposed to call the get method but it doesn't seem to be. I know because i did and echo out and i got nothing displayed. I get the problem on my set method because for some reason _changedproperties is being passed to it and not the correct values. Does someone know why please? class Users { private $_properties; private $_hdb; public function __construct($userID) { $this->_properties = array(); $this->_changedProperties = array(); $this->_properties['id'] = null; $this->_properties['surname'] = null; $this->_properties['forename'] = null; $this->_hdb = mysql_connect('localhost','username','password'); if(! is_resource($this->_hdb)) { throw new Exception("Unable to connect to the database"); } $connected = mysql_select_db('phpDevelopment',$this->_hdb); if (! $connected) { throw new Exception("Unable to use the database"); } $sql = "SELECT * FROM users WHERE UserID = $userID"; $rs = mysql_query($sql,$this->_hdb); if (! mysql_num_rows($rs)) { throw new Exception("No User exists"); } $this->_properties['id'] = $row['UserID']; $this->_properties['surname'] = $row['Surname']; $this->_properties['forename'] = $row['Forename']; } function __get($propertyName) { echo "dsdsdsd"; if (!array_key_exists($propertyName, $this->_properties)) throw new Exception('Invalid property value'); if (method_exists($this, 'get' . $propertyName)) { return call_user_func(array($this, 'get' . $propertyName)); } else { return $this->_Properties[$propertyName]; } } function __set($propertyName,$value) { if(!array_key_exists($propertyName, $this->_properties)) throw new Exception ('Invalid property Value'); if(method_exists($this, 'set' . $propertyName)) { return call_user_func(array($this, 'set' . $propertyName), $value); } else { $this->_Properties[$propertyName] = $value; } } function setID($value) { throw new Exception("User ID cannot be modified"); } function sayHello() { print "My name is {$this->forename} . {$this->surname} and my ID is {$this->id}"; } } Quote Link to comment https://forums.phpfreaks.com/topic/54572-object-oriented-get-set-using-constructor/ Share on other sites More sharing options...
snipered Posted June 7, 2007 Author Share Posted June 7, 2007 I've moved this to the OO section. Quote Link to comment https://forums.phpfreaks.com/topic/54572-object-oriented-get-set-using-constructor/#findComment-269886 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.