Facade packaging class - solves the long namespace calling problem in the view spring namespace qq space naming global namespace

WBOY
Release: 2016-07-29 08:52:56
Original
909 people have browsed it

有时候模版里面定义需要写很长的全路径类名,此处提供一个 简易的别名调用代码来规避此问题,缺点就是IDE 代码提示功能就没有了

<?php
/**
 * Facade 包装类
 *
 * 优点:
 * 		使用简单的方式来省掉视图里面长长的命名空间调用,诸如 YII 的模版
 * 缺点:
 * 		这么写在 IDE 中会失去代码自动提示的功能,对于 sublime 狗而言貌似不是缺点
 *
 * 使用方法:
 * 		ZGFacade::{方法名}( {facade名称}, 参数1, 参数2,...参数n )
 * 
 * <code>
 * // 初始化
 * ZGFacade::setZGFacade('form', 'Aert_Form');
 * ZGFacade::setZGFacade('esClient', '\Elasticsearch\Client');
 * 
 * // 使用demo
 * $form = ZGFacade::newInstance('form', 'frm2', 'delete');
 * dump($form);
 * 
 * echo ZGFacade::server('form', 'REQUEST_METHOD');
 * echo ZGFacade::get('form', 'a');
 * 
 * 	$dsn      = Config::get('esken.dsn');
 * 	$esClient = ZGFacade::newInstance('esClient', $dsn);
 * 	dump($esClient);
 * </code>
 * @author vb2005xu@qq.com
 */
final class ZGFacade
{
	private static $map = [
		'form'	=> 'Aert_Form'
	];

	public static function setZGFacade($alias, $class)
	{
		self::$map[ $alias ] = $class;
	}

	private static function __facade__($facade, $method, $arguments=[])
	{
		if ( is_object($facade) )
		{
			// 调用 对象方法
			return call_user_func_array( [$facade, $method], $arguments );
		}
		else if (is_string($facade))
		{
			if ( empty(self::$map[$facade]) )
			{
				throw new Exception("未定义 'facade': {$facade} ");
			}
			// 调用 静态方法
			if ( 'newInstance' == $method )
	    	{
	    		$class = new ReflectionClass( self::$map[$facade] );
	    		return $class->newInstanceArgs( $arguments );
	    	}
			$class = self::$map[$facade];
			return call_user_func_array( [$class, $method], $arguments );
		}

		throw new Exception("无效 'facade' 调用!");
	}

	public function __call($method, $arguments) 
    {
    	$facade = array_shift($arguments);
    	return self::__facade__($facade, $method, $arguments);
    }

    public static function __callStatic($method, $arguments) 
    {
    	$facade = array_shift($arguments);    	
    	return self::__facade__($facade, $method, $arguments);
    }

}
Copy after login

The above introduces the Facade wrapper class - solving the problem of long namespace calls in the view, including namespace and acad aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!