I have the following code :
<?php
class ShopProduct
{
private $id = 0;
private $title;
private $producerMainName;
private $producerFirstName;
protected $price;
private $discount = 0;
//constructor
public function __construct($title, $first_name, $mainName, $price)
{
$this->title = $title;
$this->producerFirstName = $first_name;
$this->producerMainName = $mainName;
$this->price = $price;
}
//getters and setters
public function getProducerFirstName()
{
return $this->producerFirstName;
}
public function getProducerMainName()
{
return $this->producerMainName;
}
public function setDiscount($num)
{
$this->discount=$num;
}
public function getDiscount()
{
return $this->discount;
}
public function getTitle()
{
return $this->title;
}
public function getPrice()
{
return ($this->price - $this->discount);
}
public function getProducer()
{
return "{$this->producerFirstName}" . " {$this->producerMainName}";
}
public function getSummaryLine()
{
$base = "{$this->title} ( {$this->producerMainName}, ";
$base .= "{$this->producerFirstName} )";
return $base;
}
public function setID($id)
{
$this->id = $id;
}
//static functions
public static function getInstance($id, PDO $pdo)
{
$stmt = $pdo->prepare("select * from products where id=?");
$result = $smtp->execute(array($id));
$row = $smtp->fetch();
if (empty($row)) {return null;}
if ($row['type'] == "book")
{
$product = new BookProduct(
$row['title'],
$row['firstname'],
$row['mainname'],
$row['price'],
$row['numpages'] );
}
else if ($row['type' == "cd"])
{
$product = new CdProduct(
$row['title'],
$row['firstname'],
$row['mainname'],
$row['price'],
$row['playlength'] );
}
else
{
$product = new ShopProduct(
$row['title'],
$row['firstname'],
$row['mainname'],
$row['price'] );
}
$product->setId($row['id']);
$product->setDiscount($row['discount']);
return $product;
}
}//end class ShopProduct
class CdProduct extends ShopProduct
{
private $playLength = 0;
public function __construct($title, $firstName, $mainName, $price, $playLength)
{
parent::__construct($title, $firstName, $mainName, $price);
$this->playLength = $playLength;
}
public function getPlayLength()
{
return $this->playLength;
}
public function getSummaryLine()
{
$base = parent::getSummaryLine();
$base .= ": playing time - {$this->playLength}";
return $base;
}
}//end class CdProduct
class BookProduct extends ShopProduct
{
private $numPages = 0;
//constructor
public function __construct($title, $firstName, $mainName, $price, $numPages)
{
parent::__construct($title, $first_name, $mainName, $price);
$this->numPages = $numPages;
}
public function getNumberOfPages()
{
return $this->numPages;
}
public function getSummaryLine()
{
$base = parent::getSummaryLine();
$base .= ": page count - {$this->numPages}";
return $base;
}
public function getPrice()
{
return $this->price;
}
}
$dsn = "sqlite://Users/skiabox/SQLite/products.db";
$pdo = new PDO($dsn, null, null);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$obj = ShopProduct::getInstance(1, $pdo);
?>
When I run this code I get this error in my browser :
Notice: Undefined variable: smtp in /Library/WebServer/Documents/MyProject11/ShopProduct.php on line 75 Call Stack: 0.0017 702736 1. {main}() /Library/WebServer/Documents/MyProject11/ShopProduct.php:0 0.0025 703800 2. ShopProduct::getInstance() /Library/WebServer/Documents/MyProject11/ShopProduct.php:188 Fatal error: Call to a member function execute() on a non-object in /Library/WebServer/Documents/MyProject11/ShopProduct.php on line 75 Call Stack: 0.0017 702736 1. {main}() /Library/WebServer/Documents/MyProject11/ShopProduct.php:0 0.0025 703800 2. ShopProduct::getInstance() /Library/WebServer/Documents/MyProject11/ShopProduct.php:188
Any ideas?
Thank you very much.