TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   base classes..... (http://www.talkphp.com/absolute-beginners/4260-base-classes.html)

allworknoplay 05-09-2009 04:28 PM

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

Wildhoney 05-09-2009 04:54 PM

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...

allworknoplay 05-09-2009 05:17 PM

Quote:

Originally Posted by Wildhoney (Post 23908)
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 05-09-2009 06:17 PM

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


Enfernikus 05-09-2009 06:26 PM

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.

allworknoplay 05-09-2009 06:38 PM

Quote:

Originally Posted by Enfernikus (Post 23911)
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...

Enfernikus 05-09-2009 06:42 PM

Yes, indeed, you could use it throught the object now

allworknoplay 05-09-2009 07:01 PM

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?

Tanax 05-09-2009 07:16 PM

Wildhoney, got any link to an exception class or a tutorial about it?

sketchMedia 05-09-2009 07:58 PM

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/

allworknoplay 05-09-2009 08:00 PM

Quote:

Originally Posted by sketchMedia (Post 23916)
Thats a PHPDoc block.
http://www.phpdoc.org/


Thanks bro!

allworknoplay 05-09-2009 09:40 PM

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.....

Tanax 05-09-2009 10:05 PM

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.

allworknoplay 05-09-2009 10:26 PM

Quote:

Originally Posted by Tanax (Post 23920)
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....

Wildhoney 05-10-2009 01:50 AM

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.

allworknoplay 05-10-2009 05:19 PM

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...

sketchMedia 05-10-2009 08:09 PM

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.


All times are GMT. The time now is 05:48 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0