英文:
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->allow_basic = implode("|", $allow_basic);
$o10->allow_basic = implode("|", $allow_basic);
</details>
# 答案1
**得分**: 1
错误信息很明确:
> 尝试在空对象上分配属性 "allow_basic"
在调用 `$o1->allow_basic` 之前,`$o1` 是空的,这导致了错误。
在使用之前,你必须将 `$o1` 定义为一个 stdClass:
```php
$o1 = new stdClass();
$o1->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->allow_basic
, which results in the error.
You have to define $o1
as a stdClass before using it:
$o1 = new stdClass();
$o1->allow_basic = 'foo';
See the PHP documentation about stdClass
: https://www.php.net/manual/en/class.stdclass.php#stdclass.properties-example
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论