Yii 1.1: Yii shorten common functions with OOP style

5 followers

This article will show how you can use Yii common functions as shorten OOP function. These functions will help you work with Yii common functions is easier as OOP style.

For examples:

<?php
 
// default function
Yii::app()->user();
// => new OOP shorten function
Hp::user();
 
// default function
Yii::app()->clientScript;
// => new OOP shorten function
Hp::cs();
 
// default function
Yii::app()->params['name'];
// => new OOP shorten function
Hp::param('name');
 
/*
// parameters as array
    'key' => array(
        'leve1_key1' => 'level 1 value 1',
        'level1_key2' => array(
                            'level2_key1' => 'values 1',
                            'level2_key2' => 'values 2',
                        ),
    ),
*/
// =>
Hp::param('key', "level1_key2", "level2_key1");
// ...

So simple and OOP style. To do that, you just get function from here: YiiOOPShorten.zip and then extract to your project structure and use them.

This is the main class, in /components/Hp.php:

<?php
 
class Hp 
{
    /**
     * Get the current user
     * @return WebUser
     */
    public static function user() {
        return Yii::app()->user;
    }
 
    /**
     * Get the current request
     * @return CHttpRequest
     */
    public static function request() {
        return Yii::app()->request;
    }
 
    /**
     * Get the current client script
     * @return client script
     */
    public static function cs() {
        return Yii::app()->clientScript;
    }
 
    /**
     * A shorthand method for getting the AssetManager 
     */
    public static function am() {
        return Yii::app()->getAssetManager();
    }
 
    /**
     * A shorthand method for getting value of a param
     * @return mixed
     */
    public static function param() {
        $value = Yii::app()->params[func_get_arg(0)];
        $totalArgs = func_num_args();
        if ($totalArgs > 1) {
            $i = 1;
            do {
                $value = $value[func_get_arg($i)];
                $i++;                
            } while ($i < $totalArgs);
        }
        return $value;
    }
 
    /**
     * An extend method for the built-in md5
     * @return string 32 bytes string
     */
    public static function md5($str) {
        if ($str)
            $str = md5($str . self::param('salt'));
        return $str;
    }
 
    /**
     * Get website's base url
     * @return string
     */
    public static function baseUrl() {
        return Yii::app()->request->baseUrl;
    }
 
    /**
     * Get website's theme url
     * @return string
     */
    public static function themeUrl() {
        return Yii::app()->theme->baseUrl;
    }
 
    /**
     * A shorthand method for creating absolute app urls
     * @param string $route
     * @param array $params
     * @return string 
     */
    public static function url($route, $params = array()) {
        return Yii::app()->createAbsoluteUrl($route, $params);
    }
 
}

Enjoy with your new functions!!!

Total 3 comments

#10705 report it
nirmalroka at 2012/11/17 01:50am
You can contribute something that realy need to us

:) :P

#10632 report it
Imre at 2012/11/09 06:38am
I am sorry, but i had to downvote this.
  • Unnecessary complecity by introducing new class unless you plan to extend it which you should not because thats what Yii::app() is for.
  • cryptic names like "cs"
  • static functions are hard to test with Unit Testing (debateable?)
  • By using class does not make your code "OOP style", usage of this wrapper is still procedural in nature.

http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

#10619 report it
marianzburlea at 2012/11/08 09:13am
Good job!

Nice idea, I'll give it a try later on.

Good job!

Leave a comment

Please to leave your comment.

Write new article
  • Written by: pdtkame
  • Category: Tips
  • Yii Version: 1.1
  • Votes: +9 / -14
  • Viewed: 5,229 times
  • Created on: Nov 8, 2012
  • Last updated: never
  • Tags: Yii shorten OOP