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 10-22-2009, 07:45 PM   #1 (permalink)
The Contributor
 
Sirupsen's Avatar
 
Join Date: May 2009
Posts: 53
Thanks: 2
Sirupsen is on a distinguished road
Bricks Static and Unstatic stuff

Hello there Talkphp'ers!

I'm currently in the process of making my own "design" or "framework", mostly to expand my knowledge with patterns. But also to try and solve some problems which comes on the way, it's turning out to be a great learning experience! Been able to solve most myself.

To learn, you need to expirement, see if stuff can be done in different ways. And that's what I just did. I made some of my functions in a class static, and accessed them statically. Made some of the variables static (those which I thought would be logical to declare static), but not all the methods are static. And they are not accessed from outside this class, only from within in the example coming below!

I'd love if anyone could explain to me if static methods ONLY are useable for situations where you'd only access a method from another class (or outside it in other ways), because then you wont have to load the entire class. (It's logical that gives better performance) So, when I should declare a class variable or method static, when is it better performance wise - or other wise? (I know how it functions with scopes)

Here's the code I made which made me think above, it works perfect. So it's mostly just a question for further coding and to improve this specific scripts, if you have other improvements except for the static stuff - shoot! I'm here to become better. :)

PHP Code:
<?php
 
/*
 *
 * Template class to handle the connection between methods and
 * the actual template view files.
 *
 */
 
final class Template {
    
// Path to view file
    
private static $template;
    
// Variables to be pushed to the template
    
private $data;
    
// Wields the instance of the registry
    
private static $registry;
 
    
// Name of controller [Class]
    
private static $controller;
    
// Name of action [Method]
    
private static $action;
 
    
/*
    *
    * The template must be created with two parameters, name
    * of the controller and name of the action [Method]
    *
    * @param     string     $controller     Name of the controller
    * @param     string     $action     Name of the action [Method]
    *
    */
 
    
public function __construct($controller$action) {    
        
// Get registry instance
        
self::$registry Registry::getinstance();
 
        
// Make these available everywhere
        
self::$controller $controller;
        
self::$action $action;
 
        
// Sets the variable $template to be the path to the
        // most relevant view file
        
self::viewFile();
    }
 
    
/*
    *
    * Stores a variable in the template
    *
    * @param     string         $variable     The name of the variable     
    * @param     string         $data         The content of the varible
    *
    */
 
    
public function __set($variable$data) {
        
$this->data[$variable] = $data;
    }
 
    
/*
    *
    * When the object is converted to a string, it prints out
    * our content.
    *
    * @return      mixed   $this->process()     The template file         
    *
    */
 
    
public function __toString() {
        if (
self::$registry->config['dev_debug']) {
            echo 
'<br/>Variables pushed to <b>Template</b><br/>';
            echo 
'<pre>';
                
print_r($this->data);
            echo 
'</pre>';
            echo 
'Now requiring <b>template files..</b><br/>';
        }
 
        
/*
         *
         * Makes our configuration array and our data array into
         * variables which then are easily accessible from the
         * view file.
         *
         */
 
        
extract($this->data);
 
        
/*
         *
         * Requires the correct top file using the getPart
         * function, as well as the correct template and
         * at last the bottom part.
         *
         */
 
        
include(self::getPart('top'));
        require(
self::$template);
        include(
self::getPart('bottom'));
 
        
// Bye!
        
if (self::$registry->config['dev_debug']) {
            echo 
'Destroyed <b>Template</b><br/>';
        }
    }
 
    
/*
     *
     * Checks for custom header or bottom parts, and returns the most
     * relevant path for the part.
     *
     */
 
    
private static function getPart($part) {
        
$view_path APPPATH 'views/' self::$controller '/';    
 
        
// If a custom header/bottom for this specific file is found, load it
        
if (file_exists($view_path $part '.' self::$action '.php')) {
           return 
$view_path $part '.' self::$action '.php';
        
// elseIf a custom header/bottom for this specific controller is found, load it
        
} elseif (file_exists($view_path $part '.php')) {
            return 
$view_path $part '.php';
        
// Else, load default
        
} else { 
            return 
APPPATH 'views/' $part '.php';
        }
    }
 
    
/*
     *
     * Fetches the view file which should be used and sets it into
     * the class wide variable $template;
     *
     */
 
    
private static function viewFile() {
        
$method_view APPPATH 'views/' self::$controller '/' self::$action '.php';
 
        
$index APPPATH'views/' self::$controller '/' 'index.php';
 
        
// If there's a view file for the specific method, use it
        
if (file_exists($method_view)) {
            
self::$template $method_view;
        
// Else use the view file for the index method
        
} elseif (file_exists($index)) {
            
// Check if the method exists
            
if (method_exists(self::$controllerself::$action)) {
                
self::$template $index;
            } else {
                require(
PBLPATH 'misc/errors/404.php');
                exit;
            }
        } else {
                throw new 
Exception('Template file for class ' self::$controller ' not created.');
        }
 
        if (
self::$registry->config['dev_debug']) {
            echo 
'Instanced <b>Template</b><br/>';
        }
    }
}
Hope to get some responses I can learn from! :)
Send a message via AIM to Sirupsen Send a message via MSN to Sirupsen Send a message via Yahoo to Sirupsen Send a message via Skype™ to Sirupsen
Sirupsen 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 04:38 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