我在加载此页面时收到此错误。 错误是这样的, 解析错误:语法错误,意外'如果' (T_IF),期待第7行/server/disk_1/websites/makemyrice/cart_update.php中的函数(T_FUNCTION) 第7行是" if(isset($ _ COOKIE [' cards']))"
<?php
class CartUpdate
{
if(isset($_COOKIE['cards']))
{
$cookie = $_COOKIE['cards'];
$cookie = stripslashes($cookie);
$savedCardArray = json_decode($cookie, true);
$variety=$savedCardArray['CART'][0];
$quantity=$savedCardArray['CART'][1];
$bran=$savedCardArray['CART'][2];
}
setcookie('total', $total);
}
function __construct()
{
}
function add()
{
$variety = test_input($_POST["variety"]);
$rice_type = test_input($_POST["rice_type"]);
$quantity = test_input($_POST["quantity"]);
$bran = test_input($_POST["bran"]);
$items[]=array($variety,$quantity,$bran,$rice_type);
$json = json_encode($items);
setcookie('cards', $json);
print_r($items);
}
function delete()
{
setcookie( 'cards', "", time()-3600);
header('location:cart.php');
}
function cartlen()
{
}
?>
答案 0 :(得分:2)
您不能将app逻辑放在类中。课程有其规则。
如果您在初始化此类时尝试调用您的代码;你必须将它们放在类构造函数中。
<?php
class CartUpdate
{
/**
* Initialize the Cart.
* @return void
*/
public function __construct() {
if(isset($_COOKIE['cards'])) {
$cookie = $_COOKIE['cards'];
$cookie = stripslashes($cookie);
$savedCardArray = json_decode($cookie, true);
$variety=$savedCardArray['CART'][0];
$quantity=$savedCardArray['CART'][1];
$bran=$savedCardArray['CART'][2];
}
setcookie('total', $total);
}
}
function add()
{
$variety = test_input($_POST["variety"]);
$rice_type = test_input($_POST["rice_type"]);
$quantity = test_input($_POST["quantity"]);
$bran = test_input($_POST["bran"]);
$items[]=array($variety,$quantity,$bran,$rice_type);
$json = json_encode($items);
setcookie('cards', $json);
print_r($items);
}
function delete()
{
setcookie( 'cards', "", time()-3600);
header('location:cart.php');
}
function cartlen()
{
}
?>