WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search


Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder



Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : PHP5: Designing And Using Interfaces
Categories : PHP, Object Oriented, Interfaces, PHP Classes, Security
Joe Crawford Jr.
Joe Crawford Jr.
Date : 2004-05-27
Grade : 4 of 5 (graded 4 times)
Viewed : 17412
Search : More Articles by Joe Crawford Jr.
Action : Grade This Article
Tools : My Favotite Articles


Submit your own code examples 
 


What is an interface?

An interface is similar to a blueprint for a house, it shows exactly how the house should be built. With an interface, you create the layout of exactly how your class should work, all types in the interface must be the same in the class. Let's look at an interface example.


The Interface


<?
interface security {

public function
isAuthorized();

}
?>


A Class Implementing The Interface.


<?
class user implements security {
// create private variables to store the users roles and tell if they are authorized.
private $roles;
private
$auth;

public function
getRolesByUser($email) {
global
$db;
// get the users roles based on their email.
$usrRoles=$db->fetch_array($db->query("SELECT roles.id, roles.title FROM roles INNER JOIN members_roles ON roles.id = members_roles.roleid INNER JOIN members ON members_roles.userid = members.id WHERE members.email='".$email."'"));

// authorize the user
if(count($usrRoles)>0) $this->auth = TRUE;

// print out the array of roles so you can see what they have
echo "<pre>";
print_r($usrRoles);
echo
"</pre>";

// loop through the roles and add them to the private $roles variable in a | (pipe) delimited format.
foreach($usrRoles as $role) {
$this->roles .= strtolower($role['title']) . '|';
}

// print the value of the private $roles variable to the screen.
echo $this->roles;
}

public function
isInRoles($role) {

// check to see if the specified role is in the users roles or not.
// if it is, print yes, otherwise print no.

if(strpos($this->roles, strtolower($role))!==FALSE) echo '<br><br>Yes<br><br>';
else echo
'<br><br>No<br><br>';
}

public function
isAuthorized() {

//if the user is authorized, print yes, otherwise print no.
echo "Authorized: ";
if(
$this->auth) echo 'Yes';
else echo
'No';

}
?>




The main file using the class and the interface.


<?
//include and setup the database object to be used
include('include/class/mysql.class.php');
$db = new mysql_db();
$db->server = 'localhost';
$db->database = 'security';
$db->user = 'root';
$db->password = '';
$db->errdisplay = 1;
$db->connect();

//include the interface file
include('include/interface/security.interface.php');

// include the class file
include('include/class/user.class.php');

//create the object
$user = new user();

// call some of the methods
$user->getRolesByUser('[email protected]');
$user->isInRoles("moderator");
$user->isAuthorized();
?>


NOTE : You can find all of the code for this article at :
http://www.weberdev.com/contentfiles/PHP5_Designing_And_Using_Interfaces.zip

I have set the auth variable to true when they have roles that are found, if you use this code and are going to implement a secure members area, you should write your own code for the login, and set the auth value there, this is for example purposes only!

So what does this interface really do? Well it will keep your classes in line, if you change the public function isAuthorized in the class file and you don't change the interface file, you will get an error thrown at you by php.

Now when first starting out with interfaces don't make the mistake that I did, you CANNOT declare any properties in an interface you can only define methods. If you try to define a property php will respond with a nice error for you.

In the code above, we created a class named user and we created an interface named security in the interface we have one function named isAuthorized() this allows us to check and see if the current user has logged in and authorized with the system. Our user class implements this interface, so within the user class we have to have a public function named isAuthorized and it should have some processing code within it.

Right about now you might be asking yourself, ok so what is the point to this whole interface stuff, it looks like double the work to me.

Well you might be right, from what I have learned I feel that when working on small projects interfaces are a personal opinion, some like them and some don't. However I can see that when you move into a large project where you have 10-20 people developing an object oriented application it would be handy to force types on methods for the classes, and make sure the a class contained certain functions that were necessary.

If you had a class for the news section of your site and you wanted to make sure your users were authorized before they added news, you could implement the security interface which in turn would require you to write an isAuthorized function for your news area. Why would you do this rather than use a security class and have the methods there? Well at certain times the isAuthorized methods may not contain the same code, you may have to check totally different information for the news area than you do for another area.

if you take the code I have attached to this article you will be able to fiddle with it and get a feel for using interfaces. Also a good article that I found to be really informational for interfaces can be found at http://www.zend.com/php5/articles/php5-interfaces.php


Instructions for the code example:

1.) Extract to a directory in your web root i.e. www.yourdomain.com/security/
2.) Create a new database 'security'
3.) Import the security.sql file
4.) load the url to the /security/ directory.
5.) try changing the email in the getRolesByUser call in the index.php file.









PHP 101 Part 7 of 15 : The Bear Necessities
Categories : PHP, Beginner Guides, Object Oriented, PHP Classes
PHP Classes And Objects: A Guide To Development
Categories : PHP, PHP Classes, Object Oriented, Beginner Guides
Building An Extensible Menu Class
Categories : PHP, PHP Classes, Object Oriented, Navigation
User Authentication With patUser (part 2)
Categories : PHP, Authentication, Security
Working with Permissions in PHP, Part 1
Categories : PHP, Security
Protecting PHP Scripts with HTTP Authorization
Categories : PHP, HTTP, Security, Authentication
Making PHP Forms Object-Oriented
Categories : PHP, HTML and PHP, Object Oriented
Exploring Session Security In PHP Web Applications
Categories : PHP, Security, Sessions, Web Applications
Building a Generic RSS Class With PHP
Categories : PHP, XML, Rich Site Summary (RSS), PHP Classes
Writing A Port Scanner In PHP
Categories : PHP, HTTP, Security
Creating a Credit Card Validation Class With PHP
Categories : PHP, Credit Cards, PHP Classes
Object Oriented Programming in PHP
Categories : PHP, Object Oriented
PHP and OOP
Categories : PHP, Object Oriented, Beginner Guides
Array Manipulation With PHP
Categories : PHP, Arrays
Use PHP to Create Dynamic pdf Files
Categories : PHP, PDF