Problems with classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Coxster
Forum Newbie
Posts: 14
Joined: Tue Nov 01, 2005 3:22 am

Problems with classes

Post 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?
TJ
Forum Newbie
Posts: 20
Joined: Thu Nov 03, 2005 10:22 pm
Location: Nottingham, UK

Post 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>";
Post Reply