Current Location: Home> Latest Articles> Encapsulate end() function when building chain array operation tool

Encapsulate end() function when building chain array operation tool

M66 2025-06-02

In PHP, chained calls are a very common programming pattern, especially when building tools with array manipulation capabilities. It can help us manipulate data concisely and efficiently through continuous calls of multiple methods. To build a flexible and easy-to-extend chain operation tool, we often need to encapsulate some common array operations, such as the end() function.

The end() function is a built-in function in PHP that moves the array pointer to the last element and returns that element. Encapsulating the end() function in the chained array operation tool allows us to easily access the last element of the array in the array operation stream, keeping the code simple and easy to read.

1. Create basic tool class

First, we need to create a basic array operation class. This class will serve as the starting point for our chained calls and contain the basic operations of the array. Here is a basic example:

 class ArrayChain
{
    protected $array;

    public function __construct(array $array)
    {
        $this->array = $array;
    }

    // Return the current array object
    public function getArray()
    {
        return $this->array;
    }
}

In this class, we define a constructor that takes an array and stores it in the $array property. We also provide a getArray() method to return the current array.

2. Encapsulate end() function

Next, we need to encapsulate the end() method in this class. When encapsulating end() , you need to make sure it can return the last element of the array and maintain the ability to chain calls.

 class ArrayChain
{
    protected $array;

    public function __construct(array $array)
    {
        $this->array = $array;
    }

    // Return the current array object
    public function getArray()
    {
        return $this->array;
    }

    // Package end() Function
    public function end()
    {
        return end($this->array); // Return the last element of the array
    }
}

The end() function directly manipulates the array in the class and returns the last element in the array. Since we did not directly change the contents of the $array array, the end() method still allows us to continue to perform subsequent chain operations.

3. Chain operation example

We can now use the encapsulated end() method to make chain calls by instantiating the ArrayChain class. Here is a simple example using the end() method:

 $array = new ArrayChain([1, 2, 3, 4, 5]);

$lastElement = $array->end(); // Get the last element of the array

echo $lastElement; // Output 5

In the above example, we get the last element of the array by calling $array->end() , and the result outputs 5 .

4. Extend more chain methods

In order to enhance the functionality of the tool, we can continue to encapsulate other common array operations for the ArrayChain class, such as first() getting the first element, push() adding elements to the end of the array, pop() deleting the last element, etc.

 class ArrayChain
{
    protected $array;

    public function __construct(array $array)
    {
        $this->array = $array;
    }

    public function getArray()
    {
        return $this->array;
    }

    public function end()
    {
        return end($this->array);
    }

    public function first()
    {
        return reset($this->array);
    }

    public function push($value)
    {
        $this->array[] = $value;
        return $this; // Keep chain calls
    }

    public function pop()
    {
        array_pop($this->array);
        return $this; // Keep chain calls
    }
}

In this way, we can extend more array manipulation methods and allow chain calls in the same line of code.

5. Advantages of chain operation

Chained calls provide a concise way to manipulate arrays, especially when performing complex array operations, which can significantly improve the readability and maintainability of the code. By encapsulating common operations such as end() , push() and pop() , we can make the code more expressive and make the operation more intuitive.

Hope this article helps you understand how to elegantly encapsulate the end() function when building chained array manipulation tools. By encapsulating end() , not only can the array operation be made simpler, but it also provides convenience for future expansion.