Uncaught Error: 尝试在空值上分配属性

huangapple go评论56阅读模式
英文:

Uncaught Error: Attempt to assign property on null

问题

我正在将代码从v7.4迁移到8.0,并在我的一个页面上遇到了这个错误,但无法解决它。我知道在php v8.0中会遇到这个错误,如迁移文档中所述。

错误:

致命错误:在/homepages/5/d742909641/htdocs/payroll/Payroll/demo/demo/allowance_setup.php的第77行抛出Uncaught Error: 尝试在空值上分配属性"allow_basic":堆栈跟踪:#0 {main} /homepages/5/d742909641/htdocs/payroll/Payroll/demo/demo/allowance_setup.php的第77行


代码:

$o1->allow_basic = implode("|", $allow_basic);
$o10->allow_basic = implode("|", $allow_basic);


<details>
<summary>英文:</summary>

I was migrating a code from v7.4 to 8.0 and encountered this error on one of my page but was unable to solve it i know in php v8.0 this error will be encountered as written in migrating docs

error:

Fatal error: Uncaught Error: Attempt to assign property "allow_basic" on null in /homepages/5/d742909641/htdocs/payroll/Payroll/demo/demo/allowance_setup.php:77 Stack trace: #0 {main} thrown in /homepages/5/d742909641/htdocs/payroll/Payroll/demo/demo/allowance_setup.php on line 77


Code:
    $o1-&gt;allow_basic = implode(&quot;|&quot;, $allow_basic);
    $o10-&gt;allow_basic = implode(&quot;|&quot;, $allow_basic);

</details>


# 答案1
**得分**: 1

错误信息很明确:

&gt; 尝试在空对象上分配属性 "allow_basic"

在调用 `$o1-&gt;allow_basic` 之前,`$o1` 是空的,这导致了错误。

在使用之前,你必须将 `$o1` 定义为一个 stdClass:

```php
$o1 = new stdClass();
$o1-&gt;allow_basic = 'foo';

在线尝试

请查看有关 stdClass 的 PHP 文档:https://www.php.net/manual/en/class.stdclass.php#stdclass.properties-example

英文:

The error message is clear:

> Attempt to assign property "allow_basic" on null

$o1 is null before you call $o1-&gt;allow_basic, which results in the error.

You have to define $o1 as a stdClass before using it:

$o1 = new stdClass();
$o1-&gt;allow_basic = &#39;foo&#39;;

Try it online

See the PHP documentation about stdClass: https://www.php.net/manual/en/class.stdclass.php#stdclass.properties-example

huangapple
  • 本文由 发表于 2023年7月7日 01:02:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定