Introduction to common functions in PHP SPL standard library _php Tutorial

Source: Internet
Author: User
Tags spl

Introduction to common functions in PHP SPL standard library


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.

Copy the code code as follows:

/*test1.php*/

  

Class Test1

{

}

/*test2.lib.php*/

  

Class Test2

{

}

/*test.php*/

  

To set the file name extension of a loadable class

Spl_autoload_extensions (". php,.inc.php,.class.php,.lib.php");

Setting Include_path,autoload to find the class file in these paths, you can add multiple paths through Path_separator

Set_include_path (Get_include_path (). Path_separator. ' libs/');

Parameter not supplied, 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 (), it will go to include_path load file (. php/.inc)

Copy the code code as follows:

/*test1.php*/

  

Class Test1

{

}

/*test.php*/

  

Set_include_path (Get_include_path (). Path_separator. ' libs/');

Spl_autoload (' test1 ');

$test 1 = new Test1 ();

Spl_autoload_call () calls all Spl_autoload_register registration functions to load the file

Copy the code code as follows:

/*test1.php*/

  

Class Test

{

Public Function GetFileName ()

{

Echo ' test1.php ';

}

}

/*test2.lib.php*/

  

Class Test

{

Public Function GetFileName ()

{

Echo ' test2.lib.php ';

}

}

/*test.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:

Copy the code code as follows:

$iterator = new Arrayiterator (Array (' recipe ' = ' pancakes ', ' egg ', ' milk ', ' flour '));

Print_r (Iterator_to_array ($iterator)); Convert an iterator element to an array

echo Iterator_count ($iterator); Count the number of iterator elements

Print_r (iterator_apply ($iterator, ' Print_item ', Array ($iterator)));//Invoking a custom function for each element of an iterator

function Print_item (Iterator $iterator)

{

Echo Strtoupper ($iterator, current ()). "\ n";

return TRUE;

}

http://www.bkjia.com/PHPjc/998361.html www.bkjia.com true http://www.bkjia.com/PHPjc/998361.html techarticle common functions in PHP SPL standard library This article mainly introduces the common functions in PHP SPL standard library, this article emphatically explains Spl_autoload_extensions (), Spl_autoload_register (...).

  • Related Article

    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.