Jump to content

Object Oriented Get Set using constructor


snipered

Recommended Posts

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}";
	}

}

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.