The three most common functions in PHP SPL standard library

Source: Internet
Author: User
This article mainly introduces the common functions introduced in PHP SPL standard library, this article emphatically explains the Spl_autoload_extensions (), Spl_autoload_register (), Spl_autoload () three functions, A friend you need can refer to the following

The PHP SPL standard library provides functions for handling such as automatic loading, iterator processing, and so on.

Spl_autoload_extensions () add spl_autoload () to load the file name extension
Spl_autoload_register () registers the function into the SPL __autoload function stack.

The code is as follows:

/*test1.php*/<?phpclass test1{}/*test2.lib.php*/<?phpclass test2{}/*test.php*/<?php//Set the file name extension of the Loadable class Spl_ Autoload_extensions (". php,.inc.php,.class.php,.lib.php");//settings Include_path,autoload will find the class file in these paths, which can be obtained by path_ Separator Add multiple Pathset_include_path (Get_include_path (). Path_separator. ' libs/');//do not provide parameters, the default implementation function is Spl_autoload () spl_autoload_register (); $test 1 = new Test1 (), $test 2 = new Test2 (); Spl_autoload () It is the default implementation of __autoload () and it will go to include_path to load the file (. php/.inc)

The code is as follows:

/*test1.php*/<?phpclass test1{}/*test.php*/<?phpset_include_path (Get_include_path (). Path_separator. ' libs/'); Spl_autoload (' test1 '); $test 1 = new Test1 (); Spl_autoload_call () Call all Spl_autoload_ Register registration function to load files

The code is as follows:

/*test1.php*/<?phpclass test{public    function GetFileName ()    {        echo ' test1.php ';    }}/* Test2.lib.php*/<?phpclass test{public    function GetFileName ()    {        echo ' test2.lib.php ';    }}/* test.php*/<?php function Loader ($classname) {    if ($classname = = ' Test1 ') {        require __dir__. '/test1.php ';    }    if ($classname = = ' Test2 ') {        require __dir__. '/test2.lib.php ';    }} Spl_autoload_register (' loader '); Spl_autoload_call (' Test2 ');  $test = new test (); $test->getfilename (); test2.lib.php

Other SPL functions are described:
class_implements-returns all interfaces implemented by the specified class.
class_parents-returns the parent class of the specified class.
Class_uses-return the traits used by the given class
iterator_apply-calls a user-defined function for each element in the iterator
iterator_count-calculating the number of elements in an iterator
iterator_to_array-copying elements from an iterator to an array
spl_autoload_functions-returns all registered __autoload () functions
spl_autoload_unregister-Unregister a registered __autoload () function
spl_classes-return all available SPL classes
spl_object_hash-returns the hash ID of the specified object

such as the iterator correlation function uses:

The code is as follows:

$iterator  = new  arrayiterator (Array (' recipe ' = ' pancakes ',  ' egg ',  ' milk ',  ' flour ')); print _r (Iterator_to_array ($iterator)); Converts an iterator element to an array of echo iterator_count ($iterator); Computes the number of iterator elements Print_r (iterator_apply ($iterator, ' Print_item ', Array ($iterator)));//Calling Custom function functions for each element of an iterator  Print_item (Iterator $iterator) {    echo  strtoupper ($iterator-current ()).  " \ n ";    return  TRUE;}

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

How PHP operates a database to determine if a table exists

How PHP uses curl to connect to websites and get information

PHP operation MySQL Database and Session dialog method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.