TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-09-2009, 04:28 PM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default base classes.....

Hey guys, so I've been pondering about what are the basic classes that would normally be used within a functioning site. I'd like to get your thoughts and opinions on them. Below is what I came up with that are necessary....no code necessary, just the basic idea/structure....


Session Class
Handles the creation of session data used throughout the site. Will also handle logging the user out when called upon. This is a stand alone parent class.

Member Class
Handles the creation of new users. This class will need to rely on the validation class to accept only "clean" input. This class will be a child class of validation class.

Login Class
Pretty simple class to handle user logins. Will also need to rely on validation class for "clean" input. Based on success, will then need to rely on session class to provide access to site. This class will be a child class of validation class and Session Class

Database Class
Pretty self explanatory class for DB access. This is a stand alone parent class.

Pagination Class
Optional class for page navigation. Would rely on database class for query results. This class would be a child class of the database class.

Validation Class
This class will clean all user input such as mysql_real_escape_string, htmlentities function, string length etc... This is a stand alone parent class.



So that's about it. Does anyone have input? Am I missing anything? Am I going about it the wrong way?
I left out an "Error" class because I haven't yet quite figured out how to handle that....
But I'm hoping I can easily implement that class once these are done...?
allworknoplay is offline  
Reply With Quote
Old 05-09-2009, 04:54 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

How about Loader and Exception classes, too?

Loader Class
Loads your classes in runtime so that you don't have to place include_once all over the place. You would do something like Loader::loadClass('Me.php');, and also attempts to automatically load classes when you make the call to a specific class.

Exception Class
Allows you to set different types of exceptions for different types of errors. Such as a base exception class, with child exception classes for everything, from database exceptions to loader exceptions and beyond...
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-09-2009, 05:17 PM   #3 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
How about Loader and Exception classes, too?

Loader Class
Loads your classes in runtime so that you don't have to place include_once all over the place. You would do something like Loader::loadClass('Me.php');, and also attempts to automatically load classes when you make the call to a specific class.

Exception Class
Allows you to set different types of exceptions for different types of errors. Such as a base exception class, with child exception classes for everything, from database exceptions to loader exceptions and beyond...
Thanks WH,

is the loader class considered "lazy loading"?

Also, how does this work exactly? If the pagination class needs access to the database, do I start the class like this?

class pagination inherits database {


}


Or do I not even have to do that? Does each class file have to call Loader::loadClass('Me.php');

Or is that done in my main header.html file?

But this sounds very ideal and I would like to implement it...

As for the exception class, we'll have to get into that afterwards, I don't know enough about that stuff...


EDIT: Also, does the "Me.php" file just include all the classes?
allworknoplay is offline  
Reply With Quote
Old 05-09-2009, 06:17 PM   #4 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Ok, after looking at some tutorials online. Here's the session class I came up with. It's pretty straight forward...

PHP Code:
<?php

    
//SESSION CLASS
    
    
       
public class sessions{

       public function 
__construct() {
    
        
session_start();
    
    }
    
    
    public function 
setVar($varName,$varValue) {
    
        if (!
$varName || !$varValue) return false;
        
        
$_SESSION[$varName] = $varValue;
    
    }
    
    public function 
getVar($varName) {
    
        return 
$_SESSION[$varName];
    
    }


    public function 
delVar($varName) {
    
        unset(
$_SESSION[$varName]);
    
    }
    
    
    public function 
logout() {
    
        while (list (
$key$val) = each ($_SESSION)) 
        { 
            
session_unregister($key); 
        } 
        
        
session_destroy();
    
    }
}
?>
allworknoplay is offline  
Reply With Quote
Old 05-09-2009, 06:26 PM   #5 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

Do not make them Children of the validation class pass the validation object to them or have the Loader class class it inside. Example.


PHP Code:

<?php

class Member
{
   private 
$validation;

     public function 
__construct()
     {
         
$this->validation Loader::LoadClass('validation');
     }
}

OR

PHP Code:
<?php

class Member
{
   private 
$validation;

     public function 
__construct(Validation $val)
     {
         
$this->validation $val;
     }
}

$validation = new Validation;
$member = new Member($validation);

Also, the logout function in the session class should be in your member handling object. Alternatively you can rename it to session_destroy_all, it seems nitpicky but it's in the effort of keeping your objects well structured.
Enfernikus is offline  
Reply With Quote
The Following User Says Thank You to Enfernikus For This Useful Post:
allworknoplay (05-09-2009)
Old 05-09-2009, 06:38 PM   #6 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Enfernikus View Post
Do not make them Children of the validation class pass the validation object to them or have the Loader class class it inside. Example.


PHP Code:

<?php

class Member
{
   private 
$validation;

     public function 
__construct()
     {
         
$this->validation Loader::LoadClass('validation');
     }
}

OR

PHP Code:
<?php

class Member
{
   private 
$validation;

     public function 
__construct(Validation $val)
     {
         
$this->validation $val;
     }
}

$validation = new Validation;
$member = new Member($validation);

Also, the logout function in the session class should be in your member handling object. Alternatively you can rename it to session_destroy_all, it seems nitpicky but it's in the effort of keeping your objects well structured.

Thank you! Nitpick away, I am always looking for better approaches. I don't see that as a nitpick at all, if it's cleaner..

I like your first option, it seems better....quick question about this part:

$this->validation = Loader::LoadClass('validation');

I take it that we can now use "$this->validation" all throughout the class right? So if I had a validation class method called: html_clean().

Then within the member class, I can do this?

$this->validation->html_clean($some_var);

Is that correct?

I will also take your suggestion and modify the logout() function for the member class and not the session class...
allworknoplay is offline  
Reply With Quote
Old 05-09-2009, 06:42 PM   #7 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

Yes, indeed, you could use it throught the object now
Enfernikus is offline  
Reply With Quote
The Following User Says Thank You to Enfernikus For This Useful Post:
allworknoplay (05-09-2009)
Old 05-09-2009, 07:01 PM   #8 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

One quick last question, what does it mean when people add the "@" in the commments?

Like this:

@name PHP 5
@param blah blah..

What is the deal with using the "@" symbol? And I take it "param" means parameters?
allworknoplay is offline  
Reply With Quote
Old 05-09-2009, 07:16 PM   #9 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Wildhoney, got any link to an exception class or a tutorial about it?
__________________
Tanax is offline  
Reply With Quote
Old 05-09-2009, 07:58 PM   #10 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
One quick last question, what does it mean when people add the "@" in the commments?

Like this:

@name PHP 5
@param blah blah..

What is the deal with using the "@" symbol? And I take it "param" means parameters?
Thats a PHPDoc block.
http://www.phpdoc.org/
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
allworknoplay (05-09-2009)
Old 05-09-2009, 08:00 PM   #11 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
Thats a PHPDoc block.
http://www.phpdoc.org/

Thanks bro!
allworknoplay is offline  
Reply With Quote
Old 05-09-2009, 09:40 PM   #12 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Hi,

I went with WH's suggestion and created a loader class called: allclass.php and put that into my "includes" folder. So my tree looks like..


/www
/www/includes/
/www/images/


I have a header.html file that calls the allclass.php like this:
(/www/header.html)

PHP Code:
Loader::loadClass('includes/allclass.php'); 
And in the allclass.php, I have it very simple with this:


PHP Code:
<?php


class Loader{

include_once(
'database.php');

}

?>

But when I run my header.html file, I get this error:


Fatal error: Class 'Loader' not found.....
allworknoplay is offline  
Reply With Quote
Old 05-09-2009, 10:05 PM   #13 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Well, that is because first of all, your Loader class does not have a function called loadClass. Secondly, in order to use Loader::loadClass you must either;

1. Declare the function loadClass as static like so:
PHP Code:
public static function loadClass($classToLoad
2. Have the Loader class instanced somewhere before you call the function, which will allow you to call Loader:loadClass without having the loadClass function as static.

Also, your pagination class should not rely on Database in order to paginate.
__________________
Tanax is offline  
Reply With Quote
Old 05-09-2009, 10:26 PM   #14 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Well, that is because first of all, your Loader class does not have a function called loadClass. Secondly, in order to use Loader::loadClass you must either;

1. Declare the function loadClass as static like so:
PHP Code:
public static function loadClass($classToLoad
2. Have the Loader class instanced somewhere before you call the function, which will allow you to call Loader:loadClass without having the loadClass function as static.

Also, your pagination class should not rely on Database in order to paginate.
Thanks, it seemed to work when included the file first in the header.html like this:

PHP Code:
<?php

    
include_once("includes/allclass.php");
    
Loader::loadClass();

?>
And in the allclass.php file:


PHP Code:
<?php

    
    
class Loader {
    
    public function 
loadClass() {
        include_once(
"database.php");
        include_once(
"sessions.php");
        include_once(
"login.php");
        include_once(
"pagination.php");
        include_once(
"member.php");
        include_once(
"validation.php");
    }

}
?>

So it is kinda interesting that I have to "include_once" the allclass.php file first, and then load it with the Loader function...kinda seems redundant don't you think?

As for the pagination file, I looked it over and it's the same class that I was working on awhile ago which DOES NOT need to use mySQL, it is DB independant, you can feed it an array. So that was my mistake sorry. I thought it needed mysql but it doesn't....
allworknoplay is offline  
Reply With Quote
Old 05-10-2009, 01:50 AM   #15 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Tanax: Take a look at the PHP documentation regarding the exceptions, and how to build your own. You're then able to distinguish between regular exceptions and more specific exceptions, such as database exceptions. With try blocks you're able to have a multitude of catch statements.

Allworknoplay: Consider a simple autoloading function such as the following, perhaps this could be more useful to you for the lazy loader:

php Code:
function __autoload($szClassName)
{
    $szFilePath = str_replace('_', '/', $szClassName) . '.php';
    include_once($szFilePath);
}

new Core_Exception();
new Core_Database_Row();

This, of course, requires you to carefully consider the file structure and naming structure of your files and their classes. I'm quite biased in this approach, because I really love the way that Zend Framework implements their file naming structure. Zend_Db_Row, for example, is instantly identifiable as being located in the file Zend/Db/Row.php.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
allworknoplay (05-10-2009)
Old 05-10-2009, 05:19 PM   #16 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Thanks WH:

I like this design by Zend too....based on the code below, all it seems to do (other than correctly create the name of the file) is to "include_once" when the main script needs to call a class right?


PHP Code:
function __autoload($szClassName)
{
    
$szFilePath str_replace('_''/'$szClassName) . '.php';
    include_once(
$szFilePath);


As oppose to this:

PHP Code:
include_once("includes/database.php");
include_once(
"includes/login.php");
include_once(
"includes/member.php");
include_once(
"includes/validation.php");
include_once(
"includes/pagination.php"); 

So with the latter, it calls everything, everytime, and the former, it only calls when called upon...hence lazy loading...


Is that correct?

Also you wrote:


new Core_Exception();
new Core_Database_Row();



Is that something specific to the Zend Framework? Do I need to call them too? Or were you just providing generic example of instantiating a class...
allworknoplay is offline  
Reply With Quote
Old 05-10-2009, 08:09 PM   #17 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
new Core_Exception();
new Core_Database_Row();
I believe they we just examples to illustrate Zend's pseudo-namespace.

Quote:
Tanax: Take a look at the PHP documentation regarding the exceptions, and how to build your own. You're then able to distinguish between regular exceptions and more specific exceptions, such as database exceptions. With try blocks you're able to have a multitude of catch statements.
You can also define a top level exception handler using
set_exception_handler


PHP Code:

function ExceptionHandler(Exception $e)
{
    die(
'Erroooooorrrr:' $e->getMessage());
}
set_exception_handler('ExceptionHandler'); 
That will catch any uncaught exceptions (i.e. ones not in a try ... catch block) this can be expanded to determine what kind of exception was passed too.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
allworknoplay (05-10-2009)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract Classes sketchMedia Advanced PHP Programming 18 02-28-2013 05:38 AM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
A Generic Singleton Base Class Theo Advanced PHP Programming 7 08-18-2008 02:25 AM
Sibling Classes not getting along trs21219 Advanced PHP Programming 3 04-27-2008 03:57 AM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM


All times are GMT. The time now is 11:15 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design