bè io sono testardo e voglio farmi tutto da solo, ad esempio questa come si può definire?
codice:
<?php
class Item{
private $itemkey;
private $itemvalue;
function __construct(&$k, &$v){
$this->itemkey = $k;
$this->itemvalue = $v;
}
public function getValue(){
return $this->itemvalue;
}
public function getKey(){
return $this->itemkey;
}
}
class Collection{
private $elements = array();
private $numElements = 0;
function __construct(){
}
function addItem($k, $v){
(int) $index = $this->numElements <> 0 ? $this->numElements : 0;
$this->elements[$index] = new Item($k, $v);
$this->increment();
}
function removeItem($k){
(array) $tmpArray = array();
for($i = 0; $i < $this->numElements; $i++){
if ($this->elements[$i]->getKey() != $k){
array_push ($tmpArray, $this->elements[$i]);
}
}
$this->decrement();
$this->elements = $tmpArray;
}
function getItem($k){
for($i = 1; $i < $this->numElements; $i++){
if ($this->elements[$i]->getKey() == $k){
return $this->elements[$i];
}
}
}
function lenght(){
//return $this->numElements;
return count($this->elements);
}
function increment(){
$this->numElements++;
}
function decrement(){
$this->numElements--;
}
}
$objCollection = new Collection();
$objCollection->addItem("chiave1", "valore1");
$objCollection->addItem("chiave2", "valore2");
$objCollection->addItem("chiave3", "valore3");
// ora lo stampo
echo $objCollection->getItem("chiave2")->getValue();
// ... lo rimuovo
$objCollection->removeItem("chiave2");
// numero elementi
echo "
numero elementi: ".$objCollection->lenght();
?>