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.

Joomla Web Development Services

Joomla web development services are the talk of the day. It has many features that overall provide ease-of-use and extensibility to user. Anywhere, anyone can make use of Joomla open source solutions. Joomla is used all over the world to make powerful websites with innovative solutions. You can easily infuse your designing ideas here and provide a "good site" to the visitors and the search engines as well.

Joomla has thousands of extensions and is highly extensible, which are available in Joomla extensions directory (and most of them are free under GPL License). Hence, for getting an exclusive site designed and developed you can make use of Joomla Web Development Services offered by the web development and designing company. They have team of designers who can make the effective utilization of tools available on Joomla. They make professional and aesthetic custom web design that in nutshell represents your organization.

By using this program web site builders can easily make corporate web site portal, Corporate intranets and extranets, Online magazines, newspapers, and publications; Government applications; E-commerce and online reservations Small business Web sites; NGO Web sites; Community-based portals; websites for institutions (both religious and educational), personal homepages and much more.

You might be thinking what about the content and other related things. Well Joomla program takes care of all this. It has the feature to add simple text, documents, audiovisual tracks/snippets, books or anything you can think of. Since, Joomla is easy to use, now many e commerce companies, web designers and developers are using this service for quickly making great websites. By using Joomla web development services, clients can maintain their site themselves as minimum instructions are written there.