I've tried to write my Property Container
design-pattern implementation.
Could anybody, please, tell me, if this code is really what I intended to write (follows the Property Container
design-pattern rules)? Is there anything that can be improved?
<?php
class PropertyContainer
{
private $PropertyContainer = array();
public function __construct() { }
public function addProperty($k, $v)
{
for($i = 0; $i < count($this->PropertyContainer); $i++) { }
$this->PropertyContainer[$k] = $v;
}
public function setProperty($k, $v)
{
while($this->PropertyContainer)
{
if(key($this->PropertyContainer) == $k) {
$this->PropertyContainer[$k] = $v;
return;
}
next($this->PropertyContainer);
}
echo "Key was not found";
}
public function getProperty($k)
{
//var_dump($this->PropertyContainer);
foreach($this->PropertyContainer as $key => $val)
{
if($key == $k) { return $val; }
}
echo "Key was not found";
return;
}
}
$pc1 = new PropertyContainer();
$pc1->addProperty("myProperty1", 31);
$pc1->addProperty("myProperty2", 32);
$pc1->addProperty("myProperty3", 33);
$pc1->setProperty("myProperty2", 7);
echo $pc1->getProperty("myProperty1") . "<br />";
echo $pc1->getProperty("myProperty2") . "<br />";
echo $pc1->getProperty("myProperty3") . "<br />";
echo "<br />";
$pc2 = new PropertyContainer();
$pc2->addProperty("myProp1", 11);
$pc2->addProperty("myProp2", 11);
$pc2->addProperty("myProperty3", "Some String");
$pc2->setProperty("myProp2", 12);
echo $pc2->getProperty("myProp1") . "<br />";
echo $pc2->getProperty("myProp2") . "<br />";
echo $pc2->getProperty("myProperty3") . "<br />";
echo "<br />" . $pc2->getProperty("myProperty5") . "<br />";
?>