首页 >后端开发 >php教程 >PHP 兑现队列类 仅供参考

PHP 兑现队列类 仅供参考

WBOY
WBOY原创
2016-06-13 10:54:08778浏览

PHP 实现队列类 仅供参考

<?php/** * PHP Class for queue * @author [email&#160;protected] * @copyright DataFrog Beijingbei  Ltd. 2011-07-25 */class Queue {	public  $length=12;	public  $queue  = array();  //  if String like this "22,23,24"    convert to array to do queue	public  $delimiter=',';	function __construct($queue=array())	{		$this->queue=$queue;	}		/**	 * @desc start queue	 * @param String  $param  new queue element	 */	public function run($param)	{		if(!is_array($this->queue)){			$this->strToQue();		}		$currentlength=$this->countqueue();  //Count  the  queue length		if($currentlength<$this->length&&$this->length>0) {			$this->queAdd($param);		}else if($this->length=0)		{			$this->queue[]=$param;		}		else {			$this->queRemove();			$this->queAdd($param);		}					return $this->queue;				}	/**	 * String like this "22,23,24"  convert to array to do queue	 * @param String $string	 * @param String $delimiter	 */	public function strToQue (){		if (empty($this->queue))		{			$this->queue=array();		}		else 		{		$this->queue=explode($this->delimiter,$this->queue);		}		 				}	/**	 * insert $node into queue	 * @param string $node	 */	private function queAdd($node){		 		array_push($this->queue,$node);		$this->countqueue();	}	private function queRemove(){		$node = array_shift($this->queue);		$this->countqueue();		return $node;	}	private function countqueue(){		$currentlength= count($this->queue);		return $currentlength;	}	function __destruct()	{		unset($this->queue);	}}//example $str='' ;            //array();$obj=new Queue ($str);$obj->length=8;  // 队列元素长度 $obj->delimiter='|';  //如果队列是字符串,则元素直接的分隔符为|$a=$obj->run('91');   //要添加到队列中的元素$a=$obj->run('92'); $a=$obj->run('93'); $a=$obj->run('94'); print_r($a); ?>

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn