Stack Overflow
Stack Overflow

Reputation: 2968

The offset parameter will be set to NULL if another value is not available what does it mean in php?

I am learning to ArrayAccess interface for my final year project. I don't know when the offset parameter of the ArrayAccess::offsetSet() is set to NULL. As stated in the php.net.

Note: The offset parameter will be set to NULL if another value is not available, like in the following example.

<?php
$arrayaccess[] = "first value";
$arrayaccess[] = "second value";
print_r($arrayaccess);
?>

The above example will output:

Array
(
    [0] => first value
    [1] => second value
)

So what is the concept of NULL here ? Can anyone tell ?

Reference Link http://php.net/manual/en/arrayaccess.offsetset.php.

Thanks !

Upvotes: 1

Views: 556

Answers (2)

Cy Rossignol
Cy Rossignol

Reputation: 16867

As we've learned, the offsetSet() method of the ArrayAccess interface provides the logic needed to handle assignment of a value to an offset of the object that implements ArrayAccess:

public function offsetSet($offset, $value) 
{
     if ($offset === null) { 
         echo "Offset is NULL!"; 
     } else {
         echo "You assigned '$value' to '$offset'."; 
     }
}

When we specify a key to assign a value to an offset of an ArrayAccess object, PHP passes the key to offsetSet():

$arrayAccess['name'] = 'Alex'; 
// Outputs: "You assigned 'Alex' to 'name'." 

However if we don't provide a key, PHP sets the value of the first parameter of offsetSet() to null:

$arrayAccess[] = 'Alex'; 
// Outputs: "Offset is NULL!" 

This syntax is similar to how an array performs a push operation when no offset is specified:

$array = []; 
$array[] = 'Alex'; 

When we implement the offsetSet() method of the ArrayAccess interface, we can choose to mimic this behavior, or we could perform a different behavior, such as throwing an exception if we don't want to support an empty offset. ArrayAccess objects don't necessarily need to copy the behavior of arrays.

Upvotes: 2

Alex Kapustin
Alex Kapustin

Reputation: 2459

You mentioned ArrayAccess, this is interface, and if you implement that in your class - you will be able to use your class as array.

You copied sentence from manual about offsetSet method

Note: The offset parameter will be set to NULL if another value is not available, like in the following example.

Example is not really correct there, so I prepare another one:

http://sandbox.onlinephpfunctions.com/code/baedfadc9bd6bbfbde5ef7152e8c4e7d4a1d99e2

output is:

this is MyTest::offsetSet offset: NULL; value: 'first value'
this is MyTest::offsetSet offset: NULL; value: 'second value'

You can see that offset parameter is NULL if you do not set it in the code, however if you use code like that:

$arrayOffset[3] = "third value";

offset parameter will be 3

UPDATE: Answering on your question:

No. If you want to support both, insertion, and updating. You should implement this logic in offsetSet method. e.g:

public function offsetSet($offset, $value)
{
    if (is_null($offset)) {
        $this->data[] = $value;
    } else {
        $this->data[$offset] = $value;
    }
}

Upvotes: 1

Related Questions