Performance Analysis and Selection Strategy of result() vs. result_array() in CodeIgniter

Dec 07, 2025 · Programming · 13 views · 7.8

Keywords: CodeIgniter | result() | result_array() | performance analysis | PHP development

Abstract: This article provides an in-depth exploration of the differences, performance characteristics, and application scenarios between the result() and result_array() methods in the CodeIgniter framework. By analyzing core source code, it reveals the polymorphic nature of the result() method as a wrapper function, supporting returns of objects, arrays, or custom class instances. The paper compares the performance differences between arrays and objects in PHP, noting that arrays generally offer slight performance advantages in most scenarios, but the choice should be based on specific application needs. With code examples, it offers best practice recommendations for real-world development, helping developers make informed decisions based on data usage patterns.

Introduction

In database operations within the CodeIgniter framework, result() and result_array() are two commonly used methods for retrieving query results. Developers often face confusion about which method is optimal, especially in performance-sensitive applications. This paper analyzes the working principles of these methods from a source code perspective, discusses their performance differences, and provides selection recommendations for practical development.

Method Functionality and Source Code Analysis

The result() method is a wrapper function that returns results in different formats based on the input parameter type. Its core logic is as follows:

public function result($type = 'object')
{
    if ($type === 'array')
    {
        return $this->result_array();
    }
    elseif ($type === 'object')
    {
        return $this->result_object();
    }
    else
    {
        return $this->custom_result_object($type);
    }
}

From the source code, it is evident that the result() method controls the return type via the $type parameter: the default value is 'object', which calls result_object() to return an array of objects; when $type is set to 'array', its behavior is entirely equivalent to directly calling result_array(). Additionally, it supports passing a custom class name to return instances of that class.

The result_array() method specifically returns results in associative array form. For example:

$result = $query->result_array();
// Return structure: Array ( [0] => Array ( [col_A] => val_1A, [col_B] => val_1B ) )

In contrast, result() by default returns an array of objects:

$result = $query->result();
// Return structure: Array ( [0] => stdClass Object ( [col_A] => val_1A, [col_B] => val_1B ) )

Performance Comparison and Selection Strategy

In terms of performance, array operations are generally more efficient than object operations. In PHP, arrays are native data structures with faster access and traversal speeds, while objects involve class instantiation and method resolution, potentially introducing slight overhead. Thus, result_array() holds a performance advantage in pure data retrieval scenarios.

However, the performance difference is often negligible in most applications, and the choice should be based on the following factors:

In practical development, it is recommended to:

  1. For generic models, such as the generic model mentioned in the question, using result_array() ensures uniform data format, facilitating subsequent processing.
  2. If dynamic switching of return types is needed, use result('array') or result('object') to provide flexibility.
  3. In performance-critical paths, validate choices through benchmarking to avoid premature optimization.

Conclusion

result() and result_array() each have their applicable scenarios in CodeIgniter. Source code analysis shows that result() offers polymorphic support through parameterized design, while result_array() is an optimized method dedicated to array returns. Performance-wise, arrays have a slight edge, but the actual choice should comprehensively consider data usage needs, code maintainability, and framework ecosystem. Developers can flexibly select based on specific application contexts; in most cases, the array format suffices and aligns with best practices in PHP development.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.