我有一个简单的php文件,包含以下内容
<?php
require ($_SERVER['DOCUMENT_ROOT'] . '/assets/includes/inc.init.php');
$username = $_GET['username'];
$password = hash('sha256', $_GET['password']);
echo $ecom->userManagement->doLogin($username, $password) == null;
这是假设打印一个简单的数字0或1。
但由于某种原因,它想告诉我ecom类已经在使用
Fatal error: Cannot declare class Ecom, because the name is already in use in /var/www/html/core/class.ecom.php on line 19
这是inc.init.php文件
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
date_default_timezone_set("UTC");
//TODO: Check Session
$config = parse_ini_file('config.ini');
$mysqli = new mysqli($config['db_host'], $config['db_user'], $config['db_password'], $config['db_name']);
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
require($_SERVER['DOCUMENT_ROOT'] . '/core/class.ecom.php');
$ecom = new Ecom($mysqli);
//TODO: Move the creation to when an item is added.
$cart = null;
if(isset($_COOKIE['cart_key'])) {
$cart = $ecom->cartManagement->getCart($_COOKIE['cart_key']);
if(!$cart->check()) {
$cart->delete();
$cart = $ecom->cartManagement->create($_SERVER['REMOTE_ADDR']);
setcookie('cart_key', $cart->key, time()+60*60*24*365, '/', 'domain.com');
}
} else {
$cart = $ecom->cartManagement->create($_SERVER['REMOTE_ADDR']);
setcookie('cart_key', $cart->key, time()+60*60*24*365, '/', 'domain.com');
}
$cart->updateExpiration();
?>
ecom类只被调用一次并声明一次。
require($_SERVER['DOCUMENT_ROOT'] . '/core/class.ecom.php');
$ecom = new Ecom($mysqli);
我有一个类似的文件可以正常工作,看起来像这样
<?php
require ($_SERVER['DOCUMENT_ROOT'] . '/assets/includes/inc.init.php');
$type = $_GET['type'];
if($type == "add") {
$cart->addItem($_GET['id']);
}
else if($type == "remove") {
$cart->removeItem($_GET['id']);
}
else if($type == "update") {
$cart->updateItems();
}
echo 0;