<?php
class Product
{
private $id;
private $name;
private $price;
private $quantity;
public function __construct()
{
$get_arguments = func_get_args();
$number_of_arguments = func_num_args();
if (method_exists($this, $method_name = '__construct' . $number_of_arguments)) {
call_user_func_array(array($this, $method_name), $get_arguments);
}
}
public function __construct1($id)
{
$this->id = $id;
}
public function __construct2($id, $name)
{
$this->id = $id;
$this->name = $name;
}
public function __construct3($id, $name, $price)
{
$this->id = $id;
$this->name = $name;
$this->price = $price;
}
public function __construct4($id, $name, $price, $quantity)
{
$this->id = $id;
$this->name = $name;
$this->price = $price;
$this->quantity = $quantity;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getPrice()
{
return $this->price;
}
public function getQuantity()
{
return $this->quantity;
}
}
$product1 = new Product();
$product2 = new Product('p02');
echo '<h4>Product 2 Info</h4>';
echo 'id: ' . $product2->getId();
$product3 = new Product('p03', 'name 3');
echo '<h4>Product 3 Info</h4>';
echo 'id: ' . $product3->getId();
echo '<br>name: ' . $product3->getName();
$product4 = new Product('p04', 'name 4', 4.5);
echo '<h4>Product 4 Info</h4>';
echo 'id: ' . $product4->getId();
echo '<br>name: ' . $product4->getName();
echo '<br>price: ' . $product4->getPrice();
$product5 = new Product('p05', 'name 5', 4.5, 5);
echo '<h4>Product 5 Info</h4>';
echo 'id: ' . $product5->getId();
echo '<br>name: ' . $product5->getName();
echo '<br>price: ' . $product5->getPrice();
echo '<br>quantity: ' . $product5->getQuantity();
?>
Product 2 Info
id: p02
Product 3 Info
id: p03
name: name 3
Product 4 Info
id: p04
name: name 4
price: 4.5
Product 5 Info
id: p05
name: name 5
price: 4.5
quantity: 5