PHP SPL Common Interface

Source: Internet
Author: User
Tags spl

There are several predefined interfaces in PHP that compare four commonly used interfaces (countable,arrayaccess,Iterator, Iteratoraggregate (converged aggregate iterator iterator)).

1. Countable interface

  As seen from the manual, the main class implementation countable can be used with the count () function.

Example:  

<?PHPclassconutme{protected $_mycount= 3;  Public function Count()    {        return $this-_mycount; }}$countable=NewConutme ();Echo Count($countable);///result is "1" and not as expected//implements interface countableclassCountMeImplementscountable{protected $_myc= 4;  Public function Count()    {        return $this-_myc; }}$countable=NewCountMe ();Echo Count($countable);//result is "4" as expected

Summarize:

Countable this interface is used to count the number of objects, and when count is called on an object, if the class (such as CountMe) does not inherit countable, it will always return 1 if the inherited countable returns the number returned by the implemented Count method.

2. Arrayaccess Interface

Very often, the framework implements this interface. Key Features: Provides an interface to access objects as you would access an array. (read more about PHP using array syntax to access objects)

3.Iterator interface

  

PHP SPL Common Interface

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.