Use of PHP SPL

Nov 22, 2016 pm 06:01 PM
php spl

PHP SPL can be seen in many frameworks and MVC. PHP SPL accounts for a large proportion in many practical applications

Double linked list

<?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);
Copy after login

Use of stack

<?php
$stack = new SplStack();  //实例化堆栈
$stack->push("a");        //向堆栈中加入数据
$stack->push("b");
$stack->push("c");
/*
$stack->offsetSet(0,&#39;C&#39;);  //堆栈的节点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位置),同时在堆栈删除该节点
Copy after login

Queue

$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);
Copy after login

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();
}
Copy after login

AppendIterator

<?php
$array_a = new ArrayIterator(array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;));  //定义两个 ArrayIterator
$array_b = new ArrayIterator(array(&#39;d&#39;,&#39;e&#39;,&#39;f&#39;));
$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
Copy after login

MultipleIterator combines the array into the entire output

$idIter = new ArrayIterator(array(&#39;01&#39;,&#39;02&#39;,&#39;03&#39;));
$nameIter =  new ArrayIterator(array(&#39;qq&#39;,&#39;ss&#39;,&#39;show&#39;));

$mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
$mit->attachIterator($idIter,"id");
$mit->attachIterator($nameIter,"name");

foreach($mit as $value){

    print_r($value);

}
Copy after login

File file, prints out the name of the current folder file

date_default_timezone_get(&#39;PRC&#39;);
$it = new FilesystemIterator(&#39;.&#39;);
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();
}
Copy after login

IteratorIterator

$array=array(&#39;value1&#39;,&#39;value2&#39;,&#39;value3&#39;,&#39;value4&#39;,&#39;value5&#39;);
$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 的值
Copy after login

prints the value of the object

class Count implements Countable{

    protected  $mycount = 4;
    public function count(){
        return $this->mycount;
    }
}

$count  = new Count();
echo count($count);
Copy after login

autoload mechanism

spl_autoload_extensions(&#39;.class.php,.php&#39;); //设定以什么扩展名结尾
set_include_path(get_include_path().PATH_SEPARATOR."autoload/"); //设定文件的目录
spl_autoload_register();
new test();
///spl_autoload_register(&#39;&#39;)可以自定义

//比如我有一个文件在 文件夹 autoload下
class test{
    public function __construct(){
        echo " this is test.class.php";
    }
}
Copy after login

SPLFILE // for the file Operation

date_default_timezone_set(&#39;PRC&#39;);
$file = new SplFileInfo(&#39;qq.txt&#39;);
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;
Copy after login


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP SPL Data Structures: The Ultimate Guide for Developers PHP SPL Data Structures: The Ultimate Guide for Developers Feb 19, 2024 pm 10:30 PM

SPL Array The SPL Array class (SplArray) is an extended PHP array implementation that provides additional features such as iterator support, key comparators, and various array operation methods (such as merge, reduce, and shuffle). Example: $arr=newSplArray();$arr[]=1;$arr[]=2;$arr[]=3;//Iterate array foreach($arras$item){echo$item.php_EOL;}SPL Stack The stack is a linear data structure that follows the last-in-first-out (LIFO) principle. The SPL stack class (SplStack) provides a stack implementation and supports push.

PHP SPL Data Structures Tutorial: Improve Your Coding Skills PHP SPL Data Structures Tutorial: Improve Your Coding Skills Feb 19, 2024 pm 07:30 PM

Introduction PHPSPL (Standard PHP Library) provides a rich set of built-in data structure classes to help you improve code efficiency and maintainability. This article will delve into the data structures in PHPSPL, covering arrays, linked lists, queues, and stacks, and provide rich examples and code snippets. Array A PHP array is an ordered collection of key-value pairs. The ArrayObject class is provided in SPL, allowing you to handle PHP arrays as objects. It provides the following advantages: iterator support, easy traversal of array elements, object access syntax, simplified access to key-value pairs, serialization support, easy persistence and data exchange $arrayObject=newArrayObject(["name"=>"John" ,"a

PHP SPL Data Structures: A Comprehensive Guide from Beginner to Expert PHP SPL Data Structures: A Comprehensive Guide from Beginner to Expert Feb 19, 2024 pm 10:42 PM

1. Introduction The PHPSPL (Standard PHP Library) module contains a collection of data structures that extend PHP's core data types. These data structures are optimized to efficiently store and process large amounts of data, thereby enhancing application performance and scalability. 2. Basic data structure array array is the most basic structure in SPL, used to store a set of key-value pairs. Unlike traditional PHP arrays, SPL arrays provide additional functionality such as sorting, traversal, and comparison. $array=newArrayObject();$array["name"]="JohnDoe";$array["age"]=30;Queue The queue stores elements in first-in, first-out (FIFO) order. they are available

PHP SPL data structure: improve your code efficiency PHP SPL data structure: improve your code efficiency Feb 19, 2024 pm 10:27 PM

Overview The PHP Standard Library (SPL) provides a series of data structure classes that can be used to manage and process data. These structures are optimized to perform common operations such as insertion, deletion, and lookup efficiently. By using SPL data structures, you can improve the efficiency, readability, and maintainability of your code. Stacks Stacks follow the last-in-first-out (LIFO) principle, which means that the last element added is removed first. The SplStack class in SPL represents a stack and provides the following methods: //Create a stack $stack=newSplStack(); //Push elements onto the stack $stack->push(10);$stack->push(20);$stack-> push(30);

PHP SPL Data Structures Guide: Basics to Advanced PHP SPL Data Structures Guide: Basics to Advanced Feb 19, 2024 pm 05:21 PM

Data structures are a cornerstone of computer science, providing efficient ways to organize and process data. PHPStandardLibrary (SPL) provides a comprehensive collection of data structures that extends the capabilities of PHP's native arrays and objects. This article will take an in-depth look at PHPSPL data structures, covering all aspects from basic to advanced. Basic data structure The most basic data structure in PHPSPL is an array. SPL extends the array class and adds the following methods: $arr=newArrayObject();//Add element $arr->append($value);//Get element $value=$arr->offsetGet($offset)

PHP SPL Data Structures: Unlocking Efficient Data Management PHP SPL Data Structures: Unlocking Efficient Data Management Feb 19, 2024 pm 07:39 PM

PHPSPL Data Structures phpStandardLibrary (SPL) provides an extensive collection of data structures that simplify data processing through predefined interfaces and classes. These data structures provide a variety of functions, including iteration, comparison, insertion, and deletion, allowing developers to manage and manipulate data efficiently. Arrays SPL provides several classes for representing arrays, including the following classes: ArrayObject: Allows object-oriented operations on ordinary PHP arrays. SplFixedArray: Provides a fixed-size array to improve performance and memory management. SplQueue: Represents a first-in-first-out (FIFO) queue. //Create an ArrayObject$a

Discover the secrets of PHP SPL data structures Discover the secrets of PHP SPL data structures Feb 20, 2024 am 08:00 AM

Data Structure Overview Data structures are specific ways of organizing and storing data to optimize access to and manipulation of the data. PHPSPL extensions enable developers to focus on business logic rather than underlying data processing by providing a range of out-of-the-box data structures. Array: ArrayObjectArrayObject is an enhanced version of the standard php array that converts it into an object to provide more functionality. It supports operations such as object iteration, array length acquisition, type coercion, and element filtering. $arr=newArrayObject([1,2,3]);foreach($arras$value){echo$value."";//Output: 123} Linked list: Lin

PHP SPL data structures: the cornerstone of modern web development PHP SPL data structures: the cornerstone of modern web development Feb 19, 2024 pm 11:06 PM

PHP's standard library provides a powerful data structure for modern WEB development, called SPL (Standard PHP Library). The SPL data structure library contains a variety of abstract data types, enabling developers to manage complex data collections efficiently and reliably. Types of SPL data structures The SPL data structure library includes the following main types: Array (ArrayObject): an iterable and sortable array type. Stack (SplStack): A last-in-first-out (LIFO) data structure. Queue (SplQueue): A first-in-first-out (FIFO) data structure. Heap (SplHeap): A priority queue that supports fast insertion and deletion operations. Mapping (SplMap): a key-value pair storage that provides

See all articles