Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Generic Accessing Functions

 
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
btgmarco
Smarty n00b


Joined: 14 Oct 2011
Posts: 1

PostPosted: Fri Oct 14, 2011 8:24 pm    Post subject: Generic Accessing Functions Reply with quote

I am working on a project, and on that I use smarty library, but on the development process I had to build too many smaty_functions and on many times that functions already exists on some helper, or something like this, so to avoid create all many functions, I and an friend we did this function, to call some static method or access some attr on some class, itīs quite simple to use, we donīt have noticed any kind of bugs.

Hope that hope.


ps: Sorry if we are posting on the wrong place


Code:


<?php
/**
*
*
* function to call some static method or static attributes and return his value, if unable to access the method or attribute, returns null
*
* Calling a method
* {caller class="Foo" method="bar" param1="fooTest" param2="barTest""}
*
* Accessing a attribute
* {caller class="Foo" attr="bar"}
*
* Accessing a attribute passing an index
* {caller class="Foo" attr="bar" index="some_index"}
*
* @param class
* @return mixed
*
* @author Renato Neto <skapemr at gmail dot com>
* @author Marco Augusto <btgmarco at gmail dot com>
*
*
 */

function smarty_function_caller($params) {
    extract($params);
   
    if (!empty($class)) {

        if (!empty($method)) {

            unset($params['class']);
            unset($params['method']);

            $reflection = new ReflectionMethod($class, $method);

            if ($reflection->isStatic()) {
                return call_user_func_array(array($class, $method), $params);
            }

        } elseif (!empty($attr) ) {

            if (isset($index)) {
                $value  = $class::$$attr;

                if (isset($value[$index]) ) {
                    return $value[$index];
                }

            }else {
                return $class::$$attr;
            }
        }
    }
    return null;
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP