<code><span><span><?php</span><span>/** * PHP Class for queue *<span> @author</span> yangqijun@live.cn *<span> @copyright</span> DataFrog Beijingbei Ltd. 2011-07-25 */</span><span><span>class</span><span>Queue</span> {</span><span>public</span><span>$length</span>=<span>12</span>; <span>//默认队列,相当于初始化队列</span><span>public</span><span>$queue</span> = <span>array</span>(); <span>// if String like this "22,23,24" convert to array to do queue </span><span>public</span><span>$delimiter</span>=<span>','</span>; <span><span>function</span><span>__construct</span><span>(<span>$queue</span>=array<span>()</span>)</span> {</span><span>$this</span>->queue=<span>$queue</span>; } <span>/** *<span> @desc</span> start queue *<span> @param</span> String $param new queue element */</span><span>public</span><span><span>function</span><span>run</span><span>(<span>$param</span>)</span> {</span><span>if</span>(!is_array(<span>$this</span>->queue)){ <span>$this</span>->strToQue();<span>//将数组视为队列 </span> } <span>$currentlength</span>=<span>$this</span>->countqueue(); <span>//Count the queue length</span><span>echo</span><span>$currentlength</span>; <span>echo</span><span>$this</span>->length.<span>'<br>'</span>; <span>if</span>(<span>$currentlength</span><<span>$this</span>->length&&<span>$this</span>->length><span>0</span>) { <span>$this</span>->queAdd(<span>$param</span>); }<span>else</span><span>if</span>(<span>$this</span>->length==<span>0</span>) <span>//如果为空队列,则将队列初始化为输入的队列</span> { <span>$param</span>=<span>empty</span>(<span>$param</span>)?<span>0</span>:<span>$param</span>; <span>$this</span>->queue[]=<span>$param</span>; } <span>else</span> { <span>for</span> (<span>$i</span>=<span>0</span>;<span>$i</span><<span>$currentlength</span>-<span>$this</span>->length-<span>1</span>;<span>$i</span>++)<span>//队列比规定的队列多,要删掉队首的元素,才能入队</span> { <span>$this</span>->queRemove(); } <span>$this</span>->queAdd(<span>$param</span>); } <span>return</span><span>$this</span>->queue; } <span>/** * String like this "22,23,24" convert to array to do queue *<span> @param</span> String $string *<span> @param</span> String $delimiter */</span><span>public</span><span><span>function</span><span>strToQue</span><span>()</span>{</span><span>if</span> (<span>empty</span>(<span>$this</span>->queue)) { <span>$this</span>->queue=<span>array</span>(); } <span>else</span> { <span>$this</span>->queue=explode(<span>$this</span>->delimiter,<span>$this</span>->queue); } } <span>/** * insert $node into queue *<span> @param</span> string $node */</span><span>private</span><span><span>function</span><span>queAdd</span><span>(<span>$node</span>)</span>{</span> array_push(<span>$this</span>->queue,<span>$node</span>); <span>$this</span>->countqueue(); } <span>private</span><span><span>function</span><span>queRemove</span><span>()</span>{</span><span>$node</span> = array_shift(<span>$this</span>->queue); <span>$this</span>->countqueue(); <span>return</span><span>$node</span>; } <span>private</span><span><span>function</span><span>countqueue</span><span>()</span>{</span><span>$currentlength</span>= count(<span>$this</span>->queue); <span>return</span><span>$currentlength</span>; } <span><span>function</span><span>__destruct</span><span>()</span> {</span><span>unset</span>(<span>$this</span>->queue); } } <span>//example </span><span>$str</span>=<span>'88|89|90|56|23|45|69|23|20|100'</span>; <span>$obj</span>=<span>new</span> Queue (<span>$str</span>); <span>$obj</span>->length=<span>8</span>; <span>// 队列元素长度 </span><span>$obj</span>->delimiter=<span>'|'</span>; <span>//如果队列是字符串,则元素直接的分隔符为| </span><span>$a</span>=<span>$obj</span>->run(<span>'91'</span>); <span>//要添加到队列中的元素 </span><span>$a</span>=<span>$obj</span>->run(<span>'92'</span>); <span>$a</span>=<span>$obj</span>->run(<span>'93'</span>); <span>$a</span>=<span>$obj</span>->run(<span>'94'</span>); print_r(<span>$a</span>); <span>?></span></span></span></code>
위 내용은 PHP와 큐 내용을 포함한 PHP 큐 클래스를 소개한 내용으로, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.