从PHP中的类创建对象会产生具有相同数据的对象

从PHP中的类创建对象会产生具有相同数据的对象,php,arrays,class,oop,object,Php,Arrays,Class,Oop,Object,我的课有问题。我写了一篇课文。当我从这个类创建对象时,创建一个对象时没有问题,但是更多的对象会导致使用上一个对象的数据初始化所有以前的对象 以下是我创建对象的方式: $a = new Item(1, 111, 123, "Laptop", "NOT HP ProBook 450 G1, E9Y47EA", "kom.", 1, 20500, 25000, 18, 12, 1); 因此,像这样,如果我将其打印到屏幕上,一切都会很好,ti将给出以下信息: Item: | 00001 | Lapto

我的课有问题。我写了一篇课文。当我从这个类创建对象时,创建一个对象时没有问题,但是更多的对象会导致使用上一个对象的数据初始化所有以前的对象

以下是我创建对象的方式:

$a = new Item(1, 111, 123, "Laptop", "NOT HP ProBook 450 G1, E9Y47EA", "kom.", 1, 20500, 25000, 18, 12, 1);
因此,像这样,如果我将其打印到屏幕上,一切都会很好,ti将给出以下信息:

Item: | 00001 | Laptop___________________ NOT HP ProBook 450 G1, E9Y47EA____________________ | Units: kom. Availability: 0001 - 20.500,00 - 25.000,00 || G: 12 months
但如果我创建另一个Item对象:

$b = new Item(2, 222, 456, "DESKTOP", "blah", "kom.", 2, 41000, 46000, 18, 12, 1);
当我回应他们时,我得到:

Item: | 00002 | DESKTOP__________________ blah______________________________________________ | Units: kom. Availability: 0002 - 41.000,00 - 46.000,00 || G: 12 months

Item: | 00002 | DESKTOP__________________ blah______________________________________________ | Units: kom. Availability: 0002 - 41.000,00 - 46.000,00 || G: 12 months
如您所见,看起来它们都是用相同的数据初始化的

有人,有什么想法吗

提前谢谢

require_once("item.interface.php");
require_once("../conf/numberFormat.config.php");

//class item

class Item implements iItem
{
private static $id; //int
private static $cipher; //int
private static $serialNumber; //big int
private static $name; //string
private static $description; //text
private static $unit; //measurment unit
private static $quantity; //int
private static $price; //double
private static $recomendedPrice; //double
private static $vat; //tiny int
private static $guarantee; //tiny int
private static $warehouse; //small int

//formatting
private static $decimalPoint;
private static $decimals;
private static $thousandSeparator;

public function Item($id, $cipher, $serialNumber, $name, $description, $unit,       $quantity, $price, $recomendedPrice,
    $vat, $guarantee, $warehouse)
{
    self::$id = $id;
    self::$cipher = $cipher;
    self::$serialNumber = $serialNumber;
    self::$name = $name;
    self::$description = $description;
    self::$unit = $unit;
    self::$quantity = $quantity;
    self::$price = $price;
    self::$recomendedPrice = $recomendedPrice;
    self::$vat = $vat;
    self::$guarantee = $guarantee;
    global $decimalPoint;
    self::$decimalPoint = $decimalPoint;
    global $decimals;
    self::$decimals = $decimals;
    global $thousandSeparator;
    self::$thousandSeparator = $thousandSeparator;
    self::$warehouse = $warehouse;
}

//set methods
public function setId($id)
{
    self::$id = $id;
}

public function setCipher($cipher)
{
    self::$cipher = $cipher;
}

public function setSerialNumber($serialNumber)
{
    self::$serialNumber = $serialNumber;
}

public function setName($name)
{
    self::$name = $name;
}

public function setDescription($description)
{
    self::$description = $description;
}

public function setUnit($unit)
{
    self::$unit = $unit;
}

public function setQuantity($quantity)
{
    self::$quantity = $quantity;
}

public function setPrice($price)
{
    self::$price = $price;
}

public function setRecomendedPrice($recomendedPrice)
{
    self::$recomendedPrice = $recomendedPrice;
}

public function setVat($vat)
{
    self::$vat = $vat;
}

public function setGuarantee($guarantee)
{
    self::$guarantee = $guarantee;
}

public function setWarehouse($warehouse)
{
    self::$warehouse = $warehouse;
}

//get methods
public function getId()
{
    return self::$id;
}

public function getCipher()
{
    return self::$cipher;
}

public function getSerialNumber()
{
    return self::$serialNumber;
}

public function getName()
{
    return self::$name;
}

public function getDescription()
{
    return self::$description;
}

public function getUnit()
{
    return self::$unit;
}

public function getQuantity()
{
    return self::$quantity;
}

public function getPrice()
{
    return self::$price;
}

public function getRecomendedPrice()
{
    return self::$recomendedPrice;
}

public function getVat()
{
    return self::$vat;
}

public function getGuarantee()
{
    return self::$guarantee;
}

public function getWarehouse()
{
    return self::$warehouse;
}

//other methods
public function toString()
{
    global $ITEM;
    return ucfirst($ITEM).": | " . sprintf("%05d", self::$id) . " | " . STR_PAD(self::$name, 25, "_") .
        " <i>" . STR_PAD(self::$description, 50, "_") . "</i> | Units: ".self::$unit." Availability: " .
        sprintf("%04d", self::$quantity) . " - " . self::formatPrice(self::$price) .
        " - " . self::formatPrice(self::$recomendedPrice) . " || G: " . self::$guarantee .
        " months";
}

public function toHTML()
{
    return false;
}

private function formatPrice($input)
{
    return number_format($input, self::$decimals, self::$decimalPoint, self::$thousandSeparator);
}
}
require_once(“item.interface.php”);
require_once(“../conf/numberFormat.config.php”);
//类项目
类项实现iItem
{
私有静态$id;//int
私有静态$cipher;//int
私有静态$serialNumber;//大整数
私有静态$name;//字符串
私有静态$description;//文本
私有静态$unit;//测量单位
私有静态$quantity;//int
私有静态$price;//双精度
私有静态$recondedprice;//双精度
私有静态$vat;//tiny int
私有静态$guarange;//极小int
私有静态$warehouse;//小int
//格式化
私有静态分点;
私有静态$小数;
私人静态千美元分离器;
公共功能项($id、$cipher、$serialNumber、$name、$description、$unit、$quantity、$price、$recomendedPrice、,
$增值税,$担保,$仓库)
{
self::$id=$id;
self::$cipher=$cipher;
self::$serialNumber=$serialNumber;
self::$name=$name;
self::$description=$description;
self::$unit=$unit;
self::$quantity=$quantity;
self::$price=$price;
self::$recondedprice=$recondedprice;
self::$vat=$vat;
self::$guarange=$guarange;
全球美元小数点;
self::$decimalPoint=$decimalPoint;
全球美元小数;
self::$decimals=$decimals;
全球千美元;
self::$thousandSeparator=$thousandSeparator;
self::$warehouse=$warehouse;
}
//设置方法
公共函数setId($id)
{
self::$id=$id;
}
公共函数setCipher($cipher)
{
self::$cipher=$cipher;
}
公共函数setSerialNumber($serialNumber)
{
self::$serialNumber=$serialNumber;
}
公共函数setName($name)
{
self::$name=$name;
}
公共函数setDescription($description)
{
self::$description=$description;
}
公共功能设置单位(单位)
{
self::$unit=$unit;
}
公共函数设置数量($quantity)
{
self::$quantity=$quantity;
}
公共功能设置价格($price)
{
self::$price=$price;
}
公共函数setRecomendedPrice($recomendedPrice)
{
self::$recondedprice=$recondedprice;
}
公共功能设置增值税($vat)
{
self::$vat=$vat;
}
公共职能担保($担保)
{
self::$guarange=$guarange;
}
公共功能设置仓库($warehouse)
{
self::$warehouse=$warehouse;
}
//获取方法
公共函数getId()
{
返回self::$id;
}
公共函数getCipher()
{
返回self::$cipher;
}
公共函数getSerialNumber()
{
返回self::$serialNumber;
}
公共函数getName()
{
返回self::$name;
}
公共函数getDescription()
{
返回self::$description;
}
公共函数getUnit()
{
返回self::$unit;
}
公共函数getQuantity()
{
返回自我::$quantity;
}
公共函数getPrice()
{
返回自我::$price;
}
公共函数getRecomendedPrice()
{
返回自我::$recomendedPrice;
}
公共函数getVat()
{
返回自我::$vat;
}
公共职能保障()
{
返回自我::$保证;
}
公共函数getWarehouse()
{
返回自我::$warehouse;
}
//其他方法
公共函数toString()
{
全球$项目;
返回ucfirst($ITEM)。“:|”。.sprintf(“%05d”,self::$id)。“|”。.STR_PAD(self::$name,25,”)。
“.STR_PAD(self:$description,50,”。“|单位:”。self:$unit。“可用性:”。
sprintf(“%04d”,self::$quantity)。“-”。self::formatPrice(self:$price)。
“-”.self::formatPrice(self:$recondedprice)。”|| G:“.self:$guarange。
“月”;
}
公共函数toHTML()
{
返回false;
}
私有函数格式价格($input)
{
返回数字\ U格式($input,self:$decimals,self:$decimalPoint,self:$TOUNANDSECTOR);
}
}

这是由于您的属性(变量)是静态的

阅读类属性


您的所有属性都是静态的,这意味着它们由类的所有元素共享。如果你想让每个对象都有自己的值,为什么要声明它们是静态的?@Dean顺便说一句,看看。一个完全无关的问题:你的构造函数太长了。请仔细阅读干净的代码和/或最佳实践,你将为自己和所有其他需要使用你的代码的人做好准备favor@kingkero你能给我推荐一本书吗?@Dean,正如我说的,它使用Java来演示;)我相信如果你搜索“php最佳实践”,你会找到更多的资源(除了那本书之外,我不知道更多)。我建议包括更多关于关键字
static
self
$this
的信息。