警告:从空价值创建默认对象,在PHP 8中,我会收到关键的未接收错误:尝试分配属性“长度”在null上
WordPress 5.9.2 和 WooCommerce 6.3.1 使用此代码:
public function calculate_shipping( $package = array() ) {
global $woocommerce;
$address['suburb'] = $package['destination']['city'];
$address['state'] = $package['destination']['state'];
$address['postcode'] = $package['destination']['postcode'];
$address['country'] = $package['destination']['country'];
$i = 0;
$totalweight = 0;
$totalprice = 0;
foreach ( $woocommerce->cart->get_cart() as $item_id => $values ) {
$_product = $values['data'];
$q = 1;
while ( $values['quantity'] >= $q ) {
$products[ $i ]->length = $_product->length;
$products[ $i ]->height = $_product->height;
$products[ $i ]->width = $_product->width;
$products[ $i ]->weight = $_product->weight;
$totalweight += $_product->weight;
$totalprice += $_product->get_price();
$q++;
$i++;
}
}
}
与长度、高度、宽度和重量相关:$products[$i]->length = $_product->length;
WordPress 5.9.2 and WooCommerce 6.3.1
Using this code:
public function calculate_shipping( $package = array() ) {
global $woocommerce;
$address['suburb'] = $package['destination']['city'];
$address['state'] = $package['destination']['state'];
$address['postcode'] = $package['destination']['postcode'];
$address['country'] = $package['destination']['country'];
$i = 0;
$totalweight = 0;
$totalprice = 0;
foreach ( $woocommerce->cart->get_cart() as $item_id => $values ) {
$_product = $values['data'];
$q = 1;
while ( $values['quantity'] >= $q ) {
$products[ $i ]->length = $_product->length;
$products[ $i ]->height = $_product->height;
$products[ $i ]->width = $_product->width;
$products[ $i ]->weight = $_product->weight;
$totalweight += $_product->weight;
$totalprice += $_product->get_price();
$q++;
$i++;
}
}
}
relating to length, height, width and weight:$products[$i]->length = $_product->length;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,让我们创建一个可以更容易测试的独立示例:
现在我们可以看到这在旧版本的PHP上做了什么,例如使用此方便的工具,该工具在多个版本上运行相同的代码。
php 7.4中的输出是这样:
因此,我们可以看到正在发生的事情是,对于每个项目,代码在分配其上的属性之前隐含地创建
stdclass
对象。因此,修复程序是为了通过编写$ products [$ i] = new STDCLASS;
在$ products [$ i] - > lentht = $ _Product- ;
在我们使用时,我们还应该创建$ propertys
作为一个空数组,$ products = [];
foreach < foreach < /代码>循环。在同一工具中运行我们的修改代码我们现在可以看到它现在从php的每个PHP版本中给出了相同的结果5.4直至8.1,没有警告。
Firstly, let's create a standalone example that can be tested more easily:
Now we can see what this did on older versions of PHP, for instance using this handy tool which runs the same code on multiple versions.
The output in PHP 7.4 is this:
So we can see that what's happening is that for each item, the code is implicitly creating a
stdClass
object, before assigning properties on it. The fix is therefore to make that creation explicit by writing$products[ $i ] = new stdClass;
just before$products[ $i ]->length = $_product->length;
While we're at it, we should also create$products
as an empty array with$products = [];
before theforeach
loop.Running our modified code in the same tool we can see that it now gives the same result on every PHP version from PHP 5.4 up to and including 8.1, with no Warnings.