In PHP object-oriented programming (OOP), although the end() function is a seemingly simple array operation function, it can easily cause undetectable problems in some scenarios. Its purpose is to move the inner pointer of the array to the last element and return the value of that element. But if you are not careful in an object-oriented context, it may lead to some unexpected behavior.
First of all, it should be clear that end() is a function that operates on the passed array by reference . It changes the internal pointer position of the array, which means:
$items = ['apple', 'banana', 'cherry'];
echo end($items); // Output cherry
This is not a problem in itself, but in OOP, if you use end() on a certain property (array) of the class inside an object method, it may inadvertently affect the state of the object.
For example, suppose you have a class as follows:
class Cart {
private array $items = ['apple', 'banana', 'cherry'];
public function getLastItem() {
return end($this->items);
}
public function currentItem() {
return current($this->items);
}
}
If you call getLastItem() and then call currentItem() :
$cart = new Cart();
echo $cart->getLastItem(); // Output cherry
echo $cart->currentItem(); // Output cherry(no apple!)
This may not be the behavior you expected. Because end() changes the position of the internal pointer, current() takes the value of the current pointer position. Such "side effects" may cause bugs in complex business logic.
A safer approach is to use array_slice() to get the last element without affecting the internal pointer:
class Cart {
private array $items = ['apple', 'banana', 'cherry'];
public function getLastItemSafe() {
$last = array_slice($this->items, -1);
return $last[0] ?? null;
}
}
This method will not modify the internal pointer state of $items and is suitable for object models with high state control requirements.
If you are dealing with URL data, you also need to pay attention to the context of using end() . For example:
class UrlManager {
private array $paths = [
'https://m66.net/home',
'https://m66.net/about',
'https://m66.net/contact'
];
public function getLastPathSegment() {
$url = end($this->paths);
$parts = explode('/', parse_url($url, PHP_URL_PATH));
return end($parts);
}
}
This class can correctly take out the last path segment of the last URL, but note: both use of end() will change the pointer position of the respective array. If $this->paths still has logic that depends on the current pointer elsewhere, problems may arise.
A safer way of writing should avoid using end() directly to modify the original array structure of class properties or passed in parameters.
end() changes the internal pointer of the array, which has side effects .
In OOP, object properties may be changed state unintentionally.
You can use array_slice() or manually compute index instead of end() to get the last element.
Be especially cautious when handling URLs, file paths, or complex data structures.
If you really need to use end() , try to avoid directly acting on class attributes, and prioritize copying the array before operating.
Correct understanding and use of end() can help you write more stable and maintainable OOP code.