Creating a Simple User Class in PHP

Many current programming languages feature the ability to use object orient design and development. PHP is one of the simplest methods of doing this and allows the programmer all the flexibility and robustness of a major language. The following will explain how to create a simple user class in PHP.

To create a simple user class in PHP, create the following user class PHP file first;

<?php
class User {
// Methods
function welcome() {
print “<html>”;
print “<head>”;
print “</head>”;
print “<body>”;
print “<p>Welcome</p>”;
print “</body>”;
print “</html>”;
}

} // End of User class definition
?>

Now that you have created the welcome() method in the user_class.php file you can call this method in your index.php file. Since this is a simple User Class demonstration, when called, it will create a new instance of the user. When you are creating a website, you can include the methods to log a user in or to check if the user is still logged in or not. To create the index.php file and call the welcome() method, type the following code and save:

<?php
// Include the file containing the User class
include (‘user_class.php’);
// Create a new instance of the User class
$newUser = new User;
// Call the checkAccess() method
$newUser->checkAccess();
// Call the welcome() method
$newUser->welcome();
?>

If you want to include additional methods in your user class the code for user_class.php would look like this:

<?php
class User {
// Methods
function checkAccess() {
//Code would go here to either log the user on, or to check that he/she is still logged on.
}
function welcome() {
print “<html>”;
print “<head>”;
print “</head>”;
print “<body>”;
print “<p>Welcome</p>”;
print “</body>”;
print “</html>”;
}
} // End of User class definition
?>