PHP functions current () & key (); incompatible with function signature

I noticed that PHP current()and key()(like other array pointer functions) take an array argument by reference:

mixed current (array & $ array)
Each array has an internal pointer to its "current" element, which is initialized by the first element inserted into the array.

After several quick checks, it seems that both current(), and key()(unlike other functions of the array pointer) take the array argument by value, thereby not causing an error when passing the return value of the function.

I came to the conclusion that it's just because current()and key()do not try to move the array pointer and, therefore, does not require that an argument passed by reference (or did it somehow transparent, quiet, secretive way). However, this bothers me a bit.

Can anyone confirm if this is an assigned functionality? I would love to use it to wrest the first element / key of the returned array, but it seems strange that PHP resolves this when in almost any circumstances a fatal error occurs (or a strict standard warning) for passing values ​​to a parameter for reference.

Function or error?


For instance:

error_reporting(-1);

function getArray(){
    return array('a', 'b', 'c');
}

var_dump( current(getArray()) );
var_dump( key(getArray())     );
var_dump( next(getArray())    );
var_dump( prev(getArray())    );
var_dump( reset(getArray())   );
var_dump( end(getArray())     );

Results:

string(1) "a"

int(0)

Strict standards: Only variables should be passed by reference ...
string(1) "b"

Strict standards: Only variables should be passed by reference ...
bool(false)

Strict standards: Only variables should be passed by reference ...
string(1) "a"

Strict standards: Only variables should be passed by reference ...
string(1) "c"
+5
source share
1

, .

, &.

PHP_FUNCTION(current)
{
    HashTable *array;
    zval **entry;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &array) == FAILURE) {
        return;
    }

    if (zend_hash_get_current_data(array, (void **) &entry) == FAILURE) {
        RETURN_FALSE;
    }
    RETURN_ZVAL(*entry, 1, 0);
}

PHP_FUNCTION(next)
{
    HashTable *array;
    zval **entry;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &array) == FAILURE) {
        return;
    }

    zend_hash_move_forward(array);

    if (return_value_used) {
        if (zend_hash_get_current_data(array, (void **) &entry) == FAILURE) {
            RETURN_FALSE;
        }

        RETURN_ZVAL(*entry, 1, 0);
    }
}

- .

, , , .

Btw,

  • Zend/zend_vm_def.h
  • Zend/zend_vm_execute.h
+2

All Articles