Search code examples
phpoopphp-8.1

Migrating PHP 7 to 8 - Object's constructor is not called


We need to migrate our live online store from PHP 7.x to PHP 8.x

My main XAMPP folder is running PHP 7.4.3, and I've installed the latest XAMPP (8.1.6) into another folder.

The main site seems to work fine when I run the PHP 8 instance, but the basket object doesn't seem to be starting.

I can tell as I can echo out $cartid from PHP 7 but not 8.

Have I missed a config option I need to edit, or has something changed in the way PHP 8.x handles global variables or objects?

basket.php

...

include ("sqlcart.php");

$cart = new basket;
...

sqlcart.php

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;
    }


Solution

  • Propably incompatible changes. From docs:

    Methods with the same name as the class are no longer interpreted as constructors. The __construct() method should be used instead.

    Source: https://www.php.net/manual/en/migration80.incompatible.php