0

I have a problem with my classes. I wrote a class item. When I create objects from this class, there is no problem when creating one, but more objects results in initializing all previous objects with the data from the last one.

This is how i create the objects:

$a = new Item(1, 111, 123, "Laptop", "NOT HP ProBook 450 G1, E9Y47EA", "kom.", 1, 20500, 25000, 18, 12, 1);

So like this everything will be fine if i print it to screen ti will give the following:

Item: | 00001 | Laptop___________________ NOT HP ProBook 450 G1, E9Y47EA____________________ | Units: kom. Availability: 0001 - 20.500,00 - 25.000,00 || G: 12 months

But if I create another Item object:

$b = new Item(2, 222, 456, "DESKTOP", "blah", "kom.", 2, 41000, 46000, 18, 12, 1);

when echoing them i get:

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

As you can see, looks like they are both initialized with equal data.

Anyone, any ideas?

Thanks in advance!

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);
}
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Dean
  • 16
  • 1
  • 3
    All your properties are `static`, which means they're shared by all elements of the class. Why are you declaring them static if you want each object to have its own values? – Barmar Jul 07 '14 at 21:55
  • 1
    @Dean BTW, have a look at [this SO thread](http://stackoverflow.com/questions/217618/). – Tobias Jul 07 '14 at 21:58
  • 1
    On a totally unrelated matter: Your constructors are awfully long. Please read up on clean code and/or best practices, you'll do your future self and everyone else who has to work with your code a favor – kero Jul 07 '14 at 22:08
  • @kingkero can you please recommend me a reading? – Dean Jul 07 '14 at 22:12
  • @Barmar, yes that was the problem, deleted the static keyword and rewrote every self:: to $this-> and now everything is fine. I can't imagine i did such a mistake... Thanks Barmar, since you're the first that commented. – Dean Jul 07 '14 at 22:13
  • 1
    @Dean As I said, [Clean Code](http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882) which uses Java to demonstrate ;) I'm sure if you search for "php best practices" you find lots more resources (I don't know any more beside that book..) – kero Jul 07 '14 at 22:15

1 Answers1

2

This is due to your properties (Variables) being static.

Read up on class properties

http://www.php.net/manual/en/language.oop5.properties.php

John
  • 912
  • 6
  • 12