Page 1 of 1

Problems with classes

Posted: Sun Nov 06, 2005 2:15 am
by Coxster
Here is my class, it's very simple as I just want to try and get used to classes and using them:

Code: Select all

<?php
	class User
	{
		// Local variables
		var $username;
		var $password;
		var $keepLoggedIn;
		
		// Constructor function
		function User()
		{
			$this->username		= "not_set";
			$this->password		= "not_set";
			$this->keepLoggedIn	= "not_set";
		}
	}
?>
in my php I use

Code: Select all

$currentUser = new User;
to create a new User object.


I then have code like this:

Code: Select all

$currentUser->password = "test";
					echo $currentUser.password . "<br>";
But this echos out

Object id #1password.

How come? Why not test? If I don't include the line with $currentUser->password = "test"; and just echo out the password I still get: Object id #1password. Why? Why not "not_set"? Also why does it display Object id #1?

Posted: Sun Nov 06, 2005 2:35 am
by TJ
You're getting the C++ dot operator and the PHP member-selector mixed up. On one line you use the correct format, but then in the echo you use C++ format, which is interpreted as the PHP dot operator for concatenating strings.

$currentUser->password = "test";
echo $currentUser->password . "<br>";