首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将PHP 7迁移到8-对象的构造函数不被调用

将PHP 7迁移到8-对象的构造函数不被调用
EN

Stack Overflow用户
提问于 2022-07-24 07:32:56
回答 1查看 146关注 0票数 -1

我们需要将我们的在线商店从PHP7.x迁移到PHP8.x

我的主XAMPP文件夹正在运行PHP7.4.3,我已经将最新的XAMPP (8.1.6)安装到另一个文件夹中。

当我运行PHP 8实例时,主站点似乎运行得很好,但是购物篮对象似乎没有启动。

我可以分辨,因为我可以从PHP7中回显$cartid,但不是8。

我是否错过了需要编辑的配置选项,或者PHP8.x处理全局变量或对象的方式发生了一些变化?

basket.php

代码语言:javascript
复制
...

include ("sqlcart.php");

$cart = new basket;
...

sqlcart.php

代码语言:javascript
复制
class basket
{
    var $items;
    var $empty;
    var $cartid;
    var $voucher_id_set;

    function basket()
    {
        global $cartid;
        global $vat_rate;
        global $voucher_id_set;
        global $outmail;
        global $conn;
        global $_COOKIE;
        global $_POST;

        $voucher_id_set = 0;

        $number = 0;

        if (isset($_COOKIE["cart_id"])) {
            $cartid = ClearString(substr($_COOKIE["cart_id"], 0, 10));

            $testcartid = $cartid;
            settype($testcartid, "integer");

            if ($testcartid > 0) {
                $strsql = "SELECT * from g_tempbasket where basket_id = '" . clearstring(substr($cartid, 0, 10)) . "'";
                $result = safedb_query($strsql);
                $number = mysqli_num_rows($result);
            } else {
                $number = 0;
            }
        } else {
            // force cart creation
            $number = 0;
        }


        if ($number == 0) {

            $todaydate = date("Y-m-d h:i:s");
            $strsql = "INSERT INTO g_tempbasket (basket_id,date) ";
            $strsql .= "VALUES (NULL,'$todaydate')";

            safedb_query($strsql);

            $newcartid = mysqli_insert_id($conn);

            if ($outmail == 1) {
                setcookie("cart_id", $newcartid, time() + (24 * 3600), "", ".website.co.uk", 1);
                setcookie("voucher_id", "", 0, "", ".website.co.uk", 1);
            } else {
                setcookie("cart_id", $newcartid, time() + (24 * 3600));
                setcookie("voucher_id", "", 0);
            }
            $cartid = $newcartid;

        }

        $strsql = "SELECT t.product_id, p.descript, p.cost, t.qty ";
        $strsql .= "FROM g_tempbasket AS tb, g_product AS p, g_tempitems AS t ";
        $strsql .= "WHERE tb.basket_id = t.basket_id ";
        $strsql .= "AND t.product_id = p.product_id ";
        $strsql .= "AND tb.basket_id = " . $cartid;

        $result = safedb_query($strsql);
        $number = mysqli_num_rows($result);

        mysqli_free_result($result);

        //$this->items=$result;

        if ($number != 0) {
            $this->empty = false;
        } else {
            $this->empty = true;
        }
    } //function 

    function additem($id, $name, $addcount)
    {
        global $cartid;

        // Get product info to add
        $strsql = "SELECT descript, cost, no_vat FROM g_product ";
        $strsql .= "WHERE product_id = '" . $id . "'";
        $prodaddresult = safedb_query($strsql);
        $prodaddrow = mysqli_fetch_assoc($prodaddresult);
        $prodname = $prodaddrow["descript"];
        $prodcost = $prodaddrow["cost"];
        $prodnovat = $prodaddrow["no_vat"];
        
        $strsql = "SELECT qty FROM g_tempitems ";
        $strsql .= "WHERE basket_id = " . $cartid;
        $strsql .= " AND product_id = '" . $id . "'";

        $result = safedb_query($strsql);
        $number = mysqli_num_rows($result);

        $strsqls = "SELECT prod_code FROM g_ship_options ";
        $strsqls .= "WHERE prod_code =  '" . $id . "'";
        $sresult = safedb_query($strsqls);
        $snumber = mysqli_num_rows($sresult);

        if ($number == 0) {
            if ($id == "" || $addcount < 1) { // Basic anti-bot validation
                header("Location: index.php");
                exit;
            }
        if ($snumber != 0) { // Item is shipping - mark in basket
            $strsql = "INSERT INTO g_tempitems ";
            $strsql .= "(basket_id, product_id, qty, shipping, descript, cost, no_vat) ";
            $strsql .= "VALUES (".$cartid.", '".$id."', ".$addcount.", 1, '".$prodname."', '".$prodcost."', '".$prodnovat."')";
          } else { // Non-shipping item
              $strsql = "INSERT INTO g_tempitems ";
            $strsql .= "(basket_id, product_id, qty, shipping, descript, cost, no_vat) ";
              $strsql .= "VALUES (".$cartid.", '".$id."', ".$addcount.", 0, '".$prodname."', '".$prodcost."', '".$prodnovat."')";
        }
        } else {
      if ($id == "") { // Basic anti-bot validation
                header("Location: index.php");
                exit;
            }
        $currow = mysqli_fetch_assoc($result);
            $current = $currow["qty"];
        $new = $current + $addcount;
        if ($new <= 0) {
            $new = 1;
            }

            $strsql = "UPDATE g_tempitems ";
            $strsql .= "SET qty = ".$new.", ";
            $strsql .= "descript = '".$prodname."', ";
            $strsql .= "cost = ".$prodcost.", ";
            $strsql .= "no_vat = ".$prodnovat." ";
            $strsql .= "WHERE basket_id = ".$cartid." ";
            $strsql .= "AND product_id = '".$id."'";
        }
        mysqli_free_result($result);
        safedb_query($strsql);

        $this->empty = false;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-24 07:44:17

可能不相容的变化。从医生那里:

与类同名的

方法不再被解释为构造函数。应该使用__construct()方法。

来源:https://www.php.net/manual/en/migration80.incompatible.php

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73096545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档