Jump to content

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

}

Link to comment
https://forums.phpfreaks.com/topic/54575-solved-oo-get-set-using-constructor/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.