I am learning how to use classes in PHP and storing them and retrieving values of the classes from a Session. But I am getting an error and not sure why.
user.php
<?php
class User
{
var $username;
var $firstname;
var $lastname;
function setFirstname($data)
{
$this->firstname = $data;
}
function getFirstname()
{
return $this->firstname;
}
}
?>
Filling Class
session_start();
include('user.php');
$user = new User;
$user->setFirstname('Gregg');
$_SESSION['user'] = $user;
Accessing Class
session_start();
echo "Welcome " . $_SESSION['user']->getFirstname();
Error I recieve
Fatal error: Unknown(): The script tried
to execute a method or access a
property of an incomplete object.
Please ensure that the class definition
[b]user[/b] of the object you are trying
to operate on was loaded _before_ the
session was started
Not too sure what is going on here. Can someone help?
Thanks.