![]() |
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...? |
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... |
Quote:
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? |
Ok, after looking at some tutorials online. Here's the session class I came up with. It's pretty straight forward...
PHP Code:
|
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:
OR PHP Code:
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. |
Quote:
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... |
Yes, indeed, you could use it throught the object now
|
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? |
Wildhoney, got any link to an exception class or a tutorial about it?
|
Quote:
http://www.phpdoc.org/ |
Quote:
Thanks bro! |
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:
PHP Code:
But when I run my header.html file, I get this error: Fatal error: Class 'Loader' not found..... |
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:
Also, your pagination class should not rely on Database in order to paginate. |
Quote:
PHP Code:
PHP Code:
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.... |
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:
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. |
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:
As oppose to this: PHP Code:
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... |
Quote:
Quote:
set_exception_handler PHP Code:
|
| 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