본문 바로가기

PHP stdClass objects gives you a "warning" as of PHP 8

이온디 2023.01.30 21:21 조회 236 댓글 0
목록 글쓰기

Trying to use undefined properties in PHP stdClass objects gives you a "warning" as of PHP 8 (it used to be a "notice" that, being realistic, you ignored).

One way to fix code that looks like the following is to use the new "null coalescing" operator (??):

$a = new stdClass;
$a->first = 1;

// This will give you a warning
$b = array($a->first, $a->second ?: NULL);

// This is fine
$c = array($a->first, $a->second ?? NULL);

For the $b line, you get these logs in the different versions of PHP:

Output for 8.0.0 - 8.0.3
Warning: Undefined property: stdClass::$second in /in/DsRb2 on line 6

Output for 5.3.0 - 7.4.16
Notice: Undefined property: stdClass::$second in /in/DsRb2 on line 6


출처 : https://todayilearned.net/2021/04/php-8-warning-when-trying-use-undefined-stdclass-properties

댓글 0

아직 댓글이 없어요

이 글에 대한 생각을 가장 먼저 남겨보세요.

댓글을 작성하려면 로그인이 필요합니다.