Returning an Array from a Function

@Fretburner:

Oh my you really try to take every word out of me, dont ya. The fact that PHP array is not object oriented bring more troubles than this ‘calling member functions on non-object’ error, I admit its not the best example I can give. One problem I frequently notice from third party scripts is that they are passing array by reference using the ampersand operator &, which suggests that PHP array aint pass by reference by default. The fact that you have to remember to use ampersand to pass PHP array by reference is another source of error that is really difficult to debug.

On the other hand, its a design flaw that PHP’s array is a combination of numeric-indexed array and associative array(linked-hashmap). This implementation severely violates single responsibility principle, the usage of an array can be ambiguous unless heavily documented. I’ve had trouble using some PHP built-in functions as they return an array that I have to read the documentation thoroughly to figure out whether the array is numeric or associative.

When I say PHP array is not object oriented, I mean more than just the syntax. Good object oriented design principle requires this separation of concerns and makes sure that each object/type exhibits one particular behavior, PHP array offers nothing like that. In Python and Ruby, they offer different classes to handle these two type of arrays. There’s also a reason why I brought up both ArrayObject and SplFixedArray, use the former for associative array/HashMap and the latter for numerically index array is a good practice. Of course you can actually use SPL’s data structure like SplLinkedList if you want to, its for advanced programmers who have strong background with data structures.

At last but not least, using PHP native arrays can promote bad programming habits of building multi-dimensional associative arrays that really should’ve been presented as object composition. One example I’ve identified is from the book ‘Zend Framework 2.0 by Example’, here’s an example of the code it uses(can you identify the dimension of this array?):


return array(
          'controllers' => array('invokables' => array('Users\\Controller\\Index' => ''Users\\Controller\\IndexController',
                                                              )
          'router' => array(
              'route' => array(
                  'users' => array)
                      'type' => 'literal',
                      'options' => array(
                          'route' => '/users',
                          'defaults' => array(
                              '_NAMESPACE_' => 'Users\\Controller',
                              'controller' => 'Index',
                              'action' => 'index',
                           ),
                       ),
                       'may_terminate' => true,
                       'child_routes' => array(
                           'default' => array(
                               'type' => 'Segment',
                               'options' => array(
                                   'route' => '/[:controller[/:action]]/',
                                   'constraints' => array(
                                       'controller' => '[a-zA-Z] [a-zA-z0-9_-]*',
                                       'action' => '[a-zA-Z] [a-zA-z0-9_-]*',
                                   ),
                                   'defaults' => array(), 
                               ),
                           ),
                       ),
                       'view_manager' => array('template_path_stack' => array('users' => __DIR__ . '/../view')),
           ),
        ),
    ),
);

Okay did you bother reading this entire code through? Do you consider the multi-dimensional array close to readable and maintainable? To me an array with more than 2 dimensions is a poor programming/design practice, and it should be strictly forbidden for an array to go for more than 3 dimension. I am not sure if this is how Zend Framework is supposed to work, but at least the way the author of the book wrote such a program demonstrates that PHP arrays are dangerous and can be easily abused. If you do have to use PHP native arrays, use it sparingly.