警告:从空价值创建默认对象,在PHP 8中,我会收到关键的未接收错误:尝试分配属性“长度”在null上

发布于 2025-01-20 21:23:40 字数 1185 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨笨の傻瓜 2025-01-27 21:23:40

首先,让我们创建一个可以更容易测试的独立示例:

class Example {
    public $length, $height, $width, $weight;
    
    public function __construct($length, $height, $width, $weight) {
        $this->length = $length;
        $this->height = $height;
        $this->width = $width;
        $this->weight = $weight;
    }
    
    public function get_price() {
        return 1.23;
    }
}

$sample_data = [
    42 => ['quantity' => 2, 'data' => new Example(42, 42, 42, 42)],
    69 => ['quantity' => 1, 'data' => new Example(69, 69, 69, 69)],
];

$i           = 0;
$totalweight = 0;
$totalprice  = 0;

foreach ( $sample_data 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++;
    }
}

var_dump($products);

现在我们可以看到这在旧版本的PHP上做了什么,例如使用此方便的工具,该工具在多个版本上运行相同的代码

php 7.4中的输出是这样:

Warning: Creating default object from empty value in /in/1Imu2 on line 31

Warning: Creating default object from empty value in /in/1Imu2 on line 31

Warning: Creating default object from empty value in /in/1Imu2 on line 31
array(3) {
  [0]=>
  object(stdClass)#3 (4) {
    ["length"]=>
    int(42)
    ["height"]=>
    int(42)
    ["width"]=>
    int(42)
    ["weight"]=>
    int(42)
  }
  [1]=>
  object(stdClass)#4 (4) {
    ["length"]=>
    int(42)
    ["height"]=>
    int(42)
    ["width"]=>
    int(42)
    ["weight"]=>
    int(42)
  }
  [2]=>
  object(stdClass)#5 (4) {
    ["length"]=>
    int(69)
    ["height"]=>
    int(69)
    ["width"]=>
    int(69)
    ["weight"]=>
    int(69)
  }
}

因此,我们可以看到正在发生的事情是,对于每个项目,代码在分配其上的属性之前隐含地创建stdclass对象。因此,修复程序是为了通过编写$ products [$ i] = new STDCLASS;$ products [$ i] - &gt; 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:

class Example {
    public $length, $height, $width, $weight;
    
    public function __construct($length, $height, $width, $weight) {
        $this->length = $length;
        $this->height = $height;
        $this->width = $width;
        $this->weight = $weight;
    }
    
    public function get_price() {
        return 1.23;
    }
}

$sample_data = [
    42 => ['quantity' => 2, 'data' => new Example(42, 42, 42, 42)],
    69 => ['quantity' => 1, 'data' => new Example(69, 69, 69, 69)],
];

$i           = 0;
$totalweight = 0;
$totalprice  = 0;

foreach ( $sample_data 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++;
    }
}

var_dump($products);

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:

Warning: Creating default object from empty value in /in/1Imu2 on line 31

Warning: Creating default object from empty value in /in/1Imu2 on line 31

Warning: Creating default object from empty value in /in/1Imu2 on line 31
array(3) {
  [0]=>
  object(stdClass)#3 (4) {
    ["length"]=>
    int(42)
    ["height"]=>
    int(42)
    ["width"]=>
    int(42)
    ["weight"]=>
    int(42)
  }
  [1]=>
  object(stdClass)#4 (4) {
    ["length"]=>
    int(42)
    ["height"]=>
    int(42)
    ["width"]=>
    int(42)
    ["weight"]=>
    int(42)
  }
  [2]=>
  object(stdClass)#5 (4) {
    ["length"]=>
    int(69)
    ["height"]=>
    int(69)
    ["width"]=>
    int(69)
    ["weight"]=>
    int(69)
  }
}

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 the foreach 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文