本文整理汇总了PHP中Factory::getProduct方法的典型用法代码示例。如果您正苦于以下问题:PHP Factory::getProduct方法的具体用法?PHP Factory::getProduct怎么用?PHP Factory::getProduct使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Factory
的用法示例。
在下文中一共展示了Factory::getProduct方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getProduct
/**
* Returns new instance of the product by cloning
*
* @return Product
*/
public function getProduct()
{
return clone $this->product;
}
}
/**
* The product
*/
class SomeProduct implements Product
{
public $name;
}
/*
* =====================================
* USING OF PROTOTYPE
* =====================================
*/
$prototypeFactory = new Factory(new SomeProduct());
$firstProduct = $prototypeFactory->getProduct();
$firstProduct->name = 'The first product';
$secondProduct = $prototypeFactory->getProduct();
$secondProduct->name = 'Second product';
print_r($firstProduct->name);
// The first product
print_r($secondProduct->name);
// Second product
示例2: __construct
}
}
class Product
{
/**
* @var integer|string
*/
protected $id;
public function __construct($id)
{
$this->id = $id;
}
/**
* @return integer|string
*/
public function getId()
{
return $this->id;
}
}
/*
* =====================================
* USING OF OBJECT POOL
* =====================================
*/
Factory::pushProduct(new Product('first'));
Factory::pushProduct(new Product('second'));
print_r(Factory::getProduct('first')->getId());
// first
print_r(Factory::getProduct('second')->getId());
// second
示例3: getName
public function getName()
{
return $this->name;
}
}
class Factory
{
protected static $products = array();
public static function pushProduct(Product $product)
{
self::$products[$product->getId()] = $product;
}
public static function getProduct($id)
{
return isset(self::$products[$id]) ? self::$products[$id] : null;
}
public static function removeProduct($id)
{
if (array_key_exists($id, self::$products)) {
unset(self::$products[$id]);
}
}
}
Factory::pushProduct(new Product('first', 'first_product'));
Factory::pushProduct(new Product('second', 'second_product'));
var_dump(Factory::getProduct('first')->getName());
var_dump(Factory::getProduct('second')->getName());
var_dump(Factory::getProduct('second'));
Factory::removeProduct('second');
var_dump(Factory::getProduct('second'));
示例4: buildProduct
parent::buildProduct();
$this->product->setName('The product of the first builder');
}
}
/**
* Second builder
*/
class SecondBuilder extends Builder
{
/**
* Creates the product
*
* @return void
*/
public function buildProduct()
{
parent::buildProduct();
$this->product->setName('The product of second builder');
}
}
/*
* =====================================
* USING OF BUILDER
* =====================================
*/
$firstDirector = new Factory(new FirstBuilder());
$secondDirector = new Factory(new SecondBuilder());
print_r($firstDirector->getProduct()->getName());
// The product of the first builder
print_r($secondDirector->getProduct()->getName());
// The product of second builder
示例5: buildProduct
{
parent::buildProduct();
$this->product->setName('The product of the first builder');
}
}
class SecondBuilder extends Builder
{
public function buildProduct()
{
parent::buildProduct();
$this->product->setName('The product of the second builder');
}
}
class Factory
{
private $_builder;
public function __construct(Builder $builder)
{
$this->_builder = $builder;
$this->_builder->buildProduct();
}
public function getProduct()
{
return $this->_builder->getProduct();
}
}
$firstDirector = new Factory(new FirstBuilder());
$secondDirector = new Factory(new SecondBuilder());
var_dump($firstDirector->getProduct()->getName());
var_dump($secondDirector->getProduct()->getName());