PHP重置static静态变量

                                                                      PHP重置static静态变量

1、代码:

 '用户注册',
			self::PERFECT_INFO 	=> '完善信息',
			self::INVITE_FIREND	=> '邀请注册',
			self::SHARE_ARTICLE	=> '分享资讯',
			self::EVERYDAY_LOGIN 	=> '每日登陆'
	];
	
	/*定义赠送虚拟币*/
	static $coin = [
			self::NEW_REGISTER 	=> 100,
			self::PERFECT_INFO 	=> 200,
			self::INVITE_FIREND	=> 100,
			self::SHARE_ARTICLE	=> 200,
			self::EVERYDAY_LOGIN	=> 50,
	];
	
	/*定义赠送最大虚拟币*/
	static $max_coin = [
			self::NEW_REGISTER 	=> 500,
			self::PERFECT_INFO 	=> 500,
			self::INVITE_FIREND	=> 500,
			self::SHARE_ARTICLE	=> 500,
			self::EVERYDAY_LOGIN 	=> 500
	];
	
	/**
	 * @todo 类初始化的时候获取送豆配置
	 */
	function __construct(){
		/*根据配置重置虚拟币信息*/
		//$givenHealthGold = ConfigModel::getConfig('given_coin');
		//模拟数据
		$givenHealthGold = '{"given_coin":{"new_register":0,"perfect_info":200,"invite_firend":100,"share_article":100,"everyday_login":10},"max_coin":{"new_register":1000,"perfect_info":1000,"invite_firend":1000,"share_article":1000,"everyday_login":1000}}';
		$givenHealthGold = json_decode($givenHealthGold, true);
		
		//操作一个静态关键字
		/* $given_money = $givenHealthGold['given_coin'];		//赠送虚拟币
		foreach ($given_money as $k => $v){
			if($v){
				$constStr = strtoupper($k);
				$x = eval("return GivenCoin::{$constStr};");
				self::$coin[$x] = $v;
			}
		} */
		
		//操作多个静态关键字
		$moneyArr = [ 'given_coin' => 'coin', 'max_coin'	=> 'max_coin' ];
		foreach ($moneyArr as $_k => $_v){
			$configVal = $givenHealthGold[$_k];		//赠送虚拟币
			foreach ($configVal as $k => $v){
				if($v){
					$constStr = strtoupper($k);
					$x = eval("return GivenCoin::{$constStr};");
					self::${$_v}[$x] = $v;
				}
			}
		}
	}
	
	/**
	 * @todo 赠送虚拟币
	 * @param unknown $uid  收益用户ID
	 * @param unknown $type	赠送类型
	 */
	public function giveCoin( $uid, $type ){
		$coin = self::$coin[$type];
		$type = self::$log[$type];
		$message = "用户[{$uid}]完成了[{$type}]奖励[{$coin}]币!";
		
		echo $message;	//加虚拟币,写日志
	}
}

2、调用:

$mGiven = new GivenCoin();
$mGiven->giveCoin(1, 1);

3、结果:

用户[1]完成了[用户注册]奖励[100]币!

4、划重点:

eval("return GivenCoin::{$constStr};");

参考地址:php中引用另一个类中常量问题

 

你可能感兴趣的:(#,PHP)