Well in PHP, the below code defines number as string types but when you use plus, minus, multiply or other algorithmic operators they are converted to the corresponding number types:
$byte = "5";
$double = "6.4";
$sum = $byte + $double;
$product = $byte * $double;
echo $sum;
echo $product;
// produces 11.4 and then 32.
Since PHP object has a magic method __toString(), I wonder if similar effect can be achieved with the following approach:
class Integer{
private $value;
public function($value){
$this->value = (int)$value;
}
public function __toString(){
return (string)$this->value;
}
}
class Double{
private $value;
private $precision;
public function __construct($value, $precision = ""){
if(!empty($precision)) $value = (int)($value * pow(10, $precision)) / pow(10, $precision);
$this->value = $value;
$this->precision = $precision;
}
public function __toString(){
return (string)$this->value;
}
}
$byte = new Integer(5);
$double = new Double(6.4);
$sum = $byte + $double;
$product = $byte * $double;
echo $sum;
echo $product;
Will the code above produce the same result? Or will it generate a syntax error/exception? Just curious.