search

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);

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位置),同时在堆栈删除该节点

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);

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(&#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

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);

}

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

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 的值

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);

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";
    }
}

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;


Statement
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
Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

See all articles

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool