February 16, 2009

Creating a Simple User Class in PHP

Most modern languages support object-oriented programming techniques, and PHP is no exception. Although many simple web development projects do not require an object-oriented approach, larger projects often do. When I'm developing a web application, and I have the choice of the programming technique to use, I normally opt for an object-oriented approach of some sort, even though I might not use all of the object-oriented capabilities made available to me by PHP.

In this article, rather than describing what objects and classes are, I'll look at a simple example which hopefully will show how you can use an object-oriented programming technique when you are developing web applications.

Consider the following very simple HTML file.

index.htm

[html]

[head]

[/head]


[body]

[p]Welcome[/p]

[/body]

[/html]

Note: the use of square brackets instead of angle brackets is simply to ensure that the tags are not processed as HTML.

If you create and open this file in a browser it will display the word 'Welcome'. To turn this into a piece of object-oriented software we could produce the following two files.

index.php

[?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 welcome() method

$newUser->welcome();

?]

user_class.php

[?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

?]

If you open index.php in a browser, it will also display the word 'Welcome', but this time it is done by creating a new instance of the User class ($newUser), and then calling the welcome() method, which is contained in the user_class.php file. Obviously, for a very simple web site like this, an object-oriented approach is way over the top, but you could add new methods to the user_class.php file to, for example, enable users to register and to log on, and so on. You could then call the methods you are interested in from index.php or from any other web pages that you create.

For example, if you create a method that checks whether a user is logged on, you could call that method from any page for which you wanted to provide access control. So if we wanted to add access control to index.php, we could change the files to:

index.php

[?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();

?]

user_class.php

[?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

?]

If the web application allows users to, for example, create documents or upload photos, you could create new classes for those requirements. This way, you can build large, complex, applications that are easily maintainable.

No comments:

Post a Comment