PHP SPL的使用
雙向鏈表
<?php
$obj = new SplDoublyLinkedList();
$obj->push(1);
$obj->push(2);
$obj->push(3);
$obj->unshift(10); //unshifit 替換雙向鏈表的首部
$obj->rewind(); // 使用current 必須調用rewind,把節點指針指向bottom節點
$obj->next(); // next 指向下一個節點
$obj->prev(); // 指針指向上一個節點
echo $obj->current(); // 指針指向當前結點
if($obj->current())
{
echo "y";
}else{
echo "n";
}
if($obj->valid()){
//如果當前節點是有效節點 valid則返回true
}
$obj->pop();
//var_dump($obj);
print_r($obj);
堆棧的使用
<?php
$stack = new SplStack(); //實例化堆棧
$stack->push("a"); //向堆棧中加入數據
$stack->push("b");
$stack->push("c");
/*
$stack->offsetSet(0,'C'); //堆棧的節點0是top 的節點,設置節點的值
$stack->rewind(); //雙向鏈表的rewind和堆棧的rewind相反,堆棧的rewind使得當前指針指向TOP所在的位置,而雙向鏈表調用之后指向bottom所在的位置
echo "qq".$stack->next(); // 堆棧的next與雙向鏈表相反
echo "re".$stack->current()."</br>";
//echo "bo".$stack->bottom()."</br>";
//echo "top".$stack->top();
print_r($stack);
*/
//從TOP開始遍歷
$stack->rewind();
while($stack->valid()){
echo $stack->key()."=>".$stack->current()."</br>";
$stack->next();
}
$pop = $stack->pop();
echo $pop;
//pop操作從堆棧里面提取出的最后一個元素(TOP位置),同時在堆棧刪除該節點
隊列
$que = new SplQueue();
$que->enqueue("a"); // 入隊列
$que->enqueue("b");
$que->enqueue("c");
//print_r($que);
echo "bottom".$que->bottom()."</br>";
echo "top".$que->top();
$que->rewind();
$que->dequeue(); //出隊列
//從 bottom 位置刪除
print_r($que);
ArrayIterator
<?php
$fruits = array(
"apple" => "apple value",
"orange" => "orange value",
"grape" => "grape value"
); //定義一個水果數組
$obj = new ArrayObject($fruits);
$it = $obj->getIterator();
// 用foreach 實現遍歷數組
foreach($it as $key => $value){
echo $key."->".$value."</br>";
}
$it->rewind(); //必須要 rewind
//用 while 來遍歷數組
while($it->valid()){
echo $it->key()."->".$it->current()."</br>";
$it->next();
}
//跳過某些元素進行打印
$it->rewind();
if($it->valid()){
$it->seek(1); //尋找到1的元素
while($it->valid()){
echo $it->key()."->".$it->current()."</br>";
$it->next();
}
}
echo "</br>";
$it->rewind();
//$it->ksort(); //進行排序 用key ,
//$it->rewind();
$it->asort(); //按value 進行排序
while($it->valid()){
echo $it->key()."->".$it->current()."</br>";
$it->next();
}
AppendIterator
<?php
$array_a = new ArrayIterator(array('a','b','c')); //定義兩個 ArrayIterator
$array_b = new ArrayIterator(array('d','e','f'));
$it = new AppendIterator();
$it->append($array_a); // 將ArrayIterator追加到Iterator里
$it->append($array_b);
foreach($it as $key => $value){
echo $key."||".$value."</br>";
}
//通過APPEND方法把迭代器對象添加到AppendIterator對象中
//把兩個數組的 數值添加到一個Interator
MultipleIterator 將數組組合成整個輸出
$idIter = new ArrayIterator(array('01','02','03'));
$nameIter = new ArrayIterator(array('qq','ss','show'));
$mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
$mit->attachIterator($idIter,"id");
$mit->attachIterator($nameIter,"name");
foreach($mit as $value){
print_r($value);
}
File文件,打印出當前文件夾文件的名稱
date_default_timezone_get('PRC');
$it = new FilesystemIterator('.');
foreach($it as $value){
echo date("Y-m-d H:i:s",$value->getMtime())."</br>";
$value->isDir()?"<dir>":"";
number_format($value->getSize());
echo $value->getFileName();
}
IteratorIterator
$array=array('value1','value2','value3','value4','value5');
$out = new Outer(new ArrayIterator($array));
foreach($out as $key => $value){
echo $key."||".$value."</br>";
}
class Outer extends IteratorIterator{
public function current(){
return parent::current()."why";
}
public function key(){
return parent::current()."not";
}
}
//可以定制key和value 的值
打印對象的值
class Count implements Countable{
protected $mycount = 4;
public function count(){
return $this->mycount;
}
}
$count = new Count();
echo count($count);
autoload機制
spl_autoload_extensions('.class.php,.php'); //設定以什么擴展名結尾
set_include_path(get_include_path().PATH_SEPARATOR."autoload/"); //設定文件的目錄
spl_autoload_register();
new test();
///spl_autoload_register('')可以自定義
//比如我有一個文件在 文件夾 autoload下
class test{
public function __construct(){
echo " this is test.class.php";
}
}
SPLFILE //對文件的操作
date_default_timezone_set('PRC');
$file = new SplFileInfo('qq.txt');
echo "file is create at".date("Y-m-d H:i:s",$file->getCTime())."</br>";
echo "file is modified at".date("Y-m-d H:i:s",$file->getMTime())."</br>";
echo "file size".$file->getSize()."kb</br>";
$fileObj = $file->openFile("r");
while($fileObj->valid()){
echo $fileObj->fgets();
}
$fileObj = null;
$file = null;
FIGHTING---EVEREY BODY

浙公網安備 33010602011771號