un esempio molto basilare ...
codice:
<?php
class Sharp_char {
private $__len = 0;
private $__str = "";
public function __construct($len = 0){
$this->__len = $len;
}
public function __call($key, $values){
if($key === 'set')
$this->__str = $this->__len ? substr($values[0], 0, $this->__len) : $values[0];
return $this->__str;
}
};
class Sharp_long {
private $__value = 0;
public function __call($key, $values){
if($key === 'set')
$this->__value = (double) $values[0];
return $this->__value;
}
};
class _CMProduct {
private function __throw(&$key) {
throw(new Exception("Undefined key: {$key}"));
}
private $__product;
public function __construct(){
$this->__product = array(
'product' => new Sharp_char(20),
'name' => new Sharp_char(20),
'serial' => new Sharp_char(8),
'bUsedhcp' => new Sharp_char(1),
'abFill' => new Sharp_char(3),
'uIp' => new Sharp_long,
'uPort' => new Sharp_long,
'uSw_ver' => new Sharp_long,
'uHw_ver' => new Sharp_long,
'uFlags' => new Sharp_long,
'uGp0' => new Sharp_long,
'uGp1' => new Sharp_long,
'uGp2' => new Sharp_long,
'uGp3' => new Sharp_long
);
}
public function __get($key){
$result = null;
if(isset($this->__product[$key]))
$result = $this->__product[$key]->get();
else
$this->__throw($key);
return $result;
}
public function __set($key, $value){
$result = null;
if(isset($this->__product[$key]))
$result = $this->__product[$key]->set($value);
else
$this->__throw($key);
return $result;
}
};
$test = new _CMProduct();
$test->product = "01234567890123456789a";
$test->abFill = "abcdefghi";
$test->uFlags = "1234.5678";
echo implode('
', array($test->product, $test->abFill, $test->uFlags));
?>
Produce:
codice:
01234567890123456789
abc
1234.5678
ma solo con PHP 5 e superiori.
Per simulare al meglio il comportamento di C# con PHP che non è Strict Typed devi fare uso massiccio di controlli, creare tipi di oggetti analoghi e sfruttare molto le eccezioni