-1

I've been finding that I'm writing a lot of code like this:

$customData1 = $user['custom_data_1'] ?? null;
$customData2 = $user['custom_data_2'] ?? null;
$customData3 = $user['custom_data_3'] ?? null;

It seems like there should be some kind of simpler syntax for this.

I'd love for this to work without error:

$customData1 = $user['custom_data_1'];

And $customData1 would be null if the index is not defined. But this presents a PHP error. Is there something similar to this:

$customData1 = $user['custom_data_1']?;

I understand using ?? when the fallback is something less unusual, but null is quite common and I don't see why there isn't a default for this operator.

I know I could also do this:

$user = (object) $user;
$customData1 = $user->custom_data_1;
$customData2 = $user->custom_data_2;
$customData3 = $user->custom_data_3;

But this kind of goes beyond what I'm actually trying to find an answer for.

6
  • 1
    Does this answer your question? Using a function to initialise a variable if it not set - PHP Commented Nov 27, 2020 at 22:37
  • I am looking for built-in PHP functionality. I'm now thinking it doesn't exist. Commented Nov 27, 2020 at 22:48
  • 2
    At least, with ?? null, you're explicitly stating that you expect the index to not exist. Explicit code is always better. If PHP didn't throw a warning when a key didn't exist, then you could easily miss typos on keys, among other things. Now in most cases you're better off using objects/DTOs (very basic objects), so your properties would exist anyway and have a default value. Commented Nov 27, 2020 at 22:49
  • Note: you could do $customData1 = @$user['custom_data_1']; (which is pretty much what you're looking for). But, seriously, don't :) Commented Nov 27, 2020 at 23:09
  • @Jeto: no, it really isn't what he's looking for... Warnings will be still generated and page will be slower. Only one right approach is to solve warning, not to close eyes before them. Commented Nov 28, 2020 at 1:27

1 Answer 1

0

If you're still willing to consider involving objects, just not the one you mentioned, the you can implement the predefined interface ArrayAccess. Something like:

class stdArray implements ArrayAccess
{
    private $container = [];

    public function __const(array $array = [])
    {
        $this->container = $array;
    }

    ...

    public function offsetGet($offset)
    {
        return $this->container[$offset] ?? null;
    }

    ...

    // So we don't have to instantiate a new Object for each array if we don't need to.
    // Just me mindful though :P
    public function replace(array $array)
    {
        return $this->container = $array;
    }
}

For example:

$arr = new stdArray($user);
$customData1 = $user['custom_data_1'];
$customData2 = $user['custom_data_2'];
$customData3 = $user['custom_data_3'];
$arr = new stdArray();
foreach ($multiArray as $subArray) {
    $arr->replace($subArray);
    $customData1 = $arr['custom_data_1'];
    $customData2 = $arr['custom_data_2'];
    $customData3 = $arr['custom_data_3'];

    // Do some operations ...
}

Haven't actually tested this, but this might/should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.