Home Backend Development PHP Tutorial Using Iterator, ArrayAccess and Countable in 24php

Using Iterator, ArrayAccess and Countable in 24php

Jul 29, 2016 am 09:01 AM
function public quot this

Iterators are often used by us to facilitate data management when reading large amounts of data in the database.

<?php
class Basket implements Iterator{
    private $fruits = array(&#39;apple&#39;, &#39;banna&#39;, &#39;pear&#39;, &#39;orange&#39;, &#39;watermelon&#39;);
    private $position = 0; 
    
    //返回当前的元素
    public function current(){
        return $this->fruits[$this->position];
    }
    //返回当前的键
    public function key(){
        return $this->position
    }
    //下移一个元素
    public function next(){
        ++$this->position;
    }
    
    //移动到第一个元素
    public function rewind(){
        $this->position = 0;
    }
    //判断后续是否还有元素
    public function valid(){
        return isset($this->fruits[$this->position+1]);
    }
    
}

Copy after login

Makes the data in the object accessible like an array

<?php
class obj implements ArrayAccess{
    private $container = array();

    public function __construct(){
        $this->container = array(
            "one" => 1,
            "two" => 2,
            "three" => 3
            
        );
    }
    
    //赋值
    public function offsetSet($offset, $value){
        if(is_null($offset)){
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
    }
    
    //某键是否存在
    public function offsetExists($offset){
        return isset($this->container[$offset]);
    }
    
    //删除键值
    public function offsetUnset($offset){
        unset($this->container[$offset]);
    }
    
    //获取键对应的值
    public function offsetGet($offset){
        return isset($this->container[$offset])?$this->container[$offset]:null;
    }
}

$obj = new obj();
var_dump(isset($obj["two"]));
var_dump($obj["two"]);
unset($obj["two"]);
var_dump(isset($obj["two"]));
$obj['two'] = "A value";

var_dump($obj['two']);
echo $obj['two'];
$obj[] = 'Append 1';
$obj[] = 'Append 2';
$obj[] = 'Append 3';
var_dump($obj);
Copy after login

Makes the object count properties

<?php 
class Basket implements Countable{
    private $fruits = array(&#39;apple&#39;, &#39;banna&#39;, &#39;pear&#39;, &#39;orange&#39;, &#39;watermelon&#39;);
    public function count(){
        return count($this->fruits);
    }
}

$basket = new Basket();
echo count($basket);
Copy after login


The above introduces the use of Iterator, ArrayAccess and Countable in 24php, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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)

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

What is the difference between the developer version and the public version of iOS? What is the difference between the developer version and the public version of iOS? Mar 01, 2024 pm 12:55 PM

Every year before Apple releases a new major version of iOS and macOS, users can download the beta version several months in advance and experience it first. Since the software is used by both the public and developers, Apple has launched developer and public versions, which are public beta versions of the developer beta version, for both. What is the difference between the developer version and the public version of iOS? Literally speaking, the developer version is a developer test version, and the public version is a public test version. The developer version and the public version target different audiences. The developer version is used by Apple for testing by developers. You need an Apple developer account to download and upgrade it.

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Detailed explanation of the role and function of the MySQL.proc table Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

An article that understands this point and catches up with 70% of front-end people An article that understands this point and catches up with 70% of front-end people Sep 06, 2022 pm 05:03 PM

A colleague got stuck due to a bug pointed by this. Vue2’s this pointing problem caused an arrow function to be used, resulting in the inability to get the corresponding props. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. Today I will share with you this link. If everything is wrong If you haven’t learned it yet, please give me a big mouth.

Let's talk about why Vue2 can access properties in various options through this Let's talk about why Vue2 can access properties in various options through this Dec 08, 2022 pm 08:22 PM

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!

The usage and function of Vue.use function The usage and function of Vue.use function Jul 24, 2023 pm 06:09 PM

Usage and Function of Vue.use Function Vue is a popular front-end framework that provides many useful features and functions. One of them is the Vue.use function, which allows us to use plugins in Vue applications. This article will introduce the usage and function of the Vue.use function and provide some code examples. The basic usage of the Vue.use function is very simple, just call it before Vue is instantiated, passing in the plugin you want to use as a parameter. Here is a simple example: //Introduce and use the plug-in

See all articles