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 04-06-2009, 08:51 AM   #1 (permalink)
Jim
The Addict
 
Jim's Avatar
 
Join Date: Nov 2007
Location: the Netherlands
Posts: 281
Thanks: 2
Jim is on a distinguished road
Default Framework: Do's and Don'ts

Hi PHP-friends!

It's been a while but I entered a new internship so I'm back :P.

Lately everybody is creating his own Framework, no-one builds websites without them. So with our combined experience we should really know how to create a good framework by now.

Just starting of with something that looks like ZF from the start will only give problems in the end. Before you start creating a framework (or any big project) you have to plan things carefully. This was one of my biggest mistakes. I created a framework which works perfectly fine and stabile, but in the end I wanted it to work differently and even nicer.

My framework runs like this:

Client requests url: /news/32-mymessage.html

index:

- Load Core
- Config
- Initialize a MySQL connection via a Factory->Singleton

core:

- Initialize the main template, start a buffer
- Use reflection to search for a "news" module which uses the IModule interface
- Load the main() function from the module via reflection

module:

- Get content - bla bla not interessting
- Get template via the core and print it with the earlier created content

core:

- Close buffer and paste that in the main template
- Dump the result

This gives a quick example of my framework. I think this works fine, but you will see my problem in the next part: file structure

./index.php
./config.php
./modules/
./modules/modulename/
./modules/modulename/index.php
./template/
./template/templatename/
./template/templatename/modulename/
./template/templatename/modulename/index.tpl
./core/
./core/core.php
./core/lib/
./core/lib/libname.lib.php

As you can see the modules and templates are seperate. I wanted this because I wanted to be able to change the complete layout by assigning a different template and not just the base. But it loses the possibility to just add a new block of functionality by just uploading a folder.

--

Basically I made this topic to exchange ideas about good structures frameworks for your website, so we can learn from eachother.
__________________
Nunchaku! Who doesn't like martial arts? =)
Send a message via MSN to Jim Send a message via Skype™ to Jim
Jim is offline  
Reply With Quote
Old 04-06-2009, 04:04 PM   #2 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

What is your take on ZF? do you think it is bloated?
allworknoplay is offline  
Reply With Quote
Old 04-07-2009, 10:04 AM   #3 (permalink)
The Contributor
 
quantumkangaroo's Avatar
 
Join Date: Feb 2008
Location: Pretoria, South Africa
Posts: 42
Thanks: 1
quantumkangaroo is an unknown quantity at this point
Default

Weve looked into going into a framework driven structure, currently we have something mixed between an MVC structure and our own creativity :S

We have 1 core controller, 4 core classes and a library of sub classes, the core initiates the system and the core classes which contain security, database, error, presentation.

Our initiation process is as follows:

1. Error Handling
2. Database
3. Security
4. Presentation

So each core class would be dependent on all the core classes that would be initiated before it.

Our file structure is as follows:

./index.php
./javascript
./images
./css
./phplib
-- controller.php
-- error.php
-- database.php
-- security.php
-- presentation.php
./phplib/classes
-- class_xml.php
-- class_pdf.php
-- class_csv.php
-- class_graphs.php
-- class_mod.php
./module1
./module2

It is a PHP5 OOP strict framework fully integrated with jQuery
__________________
virtueCart v1.0.5 developed by WebDevSA

Send a message via MSN to quantumkangaroo Send a message via Skype™ to quantumkangaroo
quantumkangaroo is offline  
Reply With Quote
Old 04-07-2009, 01:51 PM   #4 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

Jim, do I understand your post right when you say you use Reflection in a production ready application for NON-debugging? That is a performance killer my friend :)
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 04-14-2009, 08:03 AM   #5 (permalink)
Jim
The Addict
 
Jim's Avatar
 
Join Date: Nov 2007
Location: the Netherlands
Posts: 281
Thanks: 2
Jim is on a distinguished road
Default

Oh? I learned about using Reflection for plugin/module systems in the book ProPHP: http://www.amazon.com/Pro-PHP-Patter.../dp/1590598199

It works like a charm though. But you still sugest I should try another way? (shouldn't be too much work I guess)
__________________
Nunchaku! Who doesn't like martial arts? =)
Send a message via MSN to Jim Send a message via Skype™ to Jim
Jim is offline  
Reply With Quote
Old 04-14-2009, 01:28 PM   #6 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

well what are you using Reflection for? Code examples?
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 04-14-2009, 02:25 PM   #7 (permalink)
Jim
The Addict
 
Jim's Avatar
 
Join Date: Nov 2007
Location: the Netherlands
Posts: 281
Thanks: 2
Jim is on a distinguished road
Default

I use it to locate and validate a module:

PHP Code:

    
/**
     * Loads a module
     *
     * @param string $szModuleName
     */
    
public function LoadModule($szModuleName
    {        
        
/*
         * A module is made with a folder in the /modules directory, 
         * from that folder the index.php folder is read
        */
        
        
$szModulePath CONF_MAIN_PATH '/modules/' basename($szModuleName) . '/index.php';
        
        if(
file_exists($szModulePath)) 
        {
            require_once(
$szModulePath);
            
            
$moduleReflection = new ReflectionClass($szModuleName);
            
            
// Get main method
            
if($moduleReflection->implementsInterface('IModule')) 
            {
                
$moduleMainMethod $moduleReflection->getMethod('Main');
                
                
// method is static, invoke 
                
if($moduleMainMethod->isStatic()) 
                {
                    
$moduleMainMethod->invoke(null);
                } 
                else 
                {
                    
$moduleInstance $moduleReflection->newInstance();
                    
$moduleMainMethod->invoke($moduleInstance);
                }                
            } 
            else 
            {
                throw new 
Exception('File is not a valid module');
            }
        } 
        else 
        {
            
// File does not exist
            
throw new Exception('Could not find module.');
        }
    } 
__________________
Nunchaku! Who doesn't like martial arts? =)
Send a message via MSN to Jim Send a message via Skype™ to Jim
Jim is offline  
Reply With Quote
Old 04-14-2009, 03:00 PM   #8 (permalink)
The Visitor
 
Join Date: Jul 2008
Posts: 1
Thanks: 0
K42B3 is on a distinguished road
Default

maybe it is faster to use the php functions class_exists, class_implements and method_exists
but I havent test that ... here my idea. Note I havent test this code.

PHP Code:
<?php

# ... your code ...

if(file_exists($szModulePath)) 
{
    require_once(
$szModulePath);
            
    if(
class_exists($szModuleNamefalse) === true)
    {
        
$interfaces class_implements($szModuleNamefalse);

        if(
$interfaces !== false && in_array('IModule'$interfaces))
        {
            if(
method_exists($szModuleName'Main') === true)
            {
                
# we have an static method available
                
$szModuleName::Main();
            }
            else
            {
                
# whether non-static or not existing method
                
$instance = new $szModuleName();
                
                if(
method_exists($instance'Main') === true)
                {
                    
$instance->Main();
                }
                else
                {
                    throw new 
Exception('main method not defined');
                }
            }
        }
        else
        {
            throw new 
Exception('File is not a valid module');
        }
    }
    else
    {
        throw new 
Exception('Could not find module.');
    }
}

?>
The fastest way is to trust your developers ... because it is necessary to prove whether a user has implement an interface?
K42B3 is offline  
Reply With Quote
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


All times are GMT. The time now is 09:04 AM.

 
     

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