Ciao, ritengo che ci sia un funzionamento anomalo del metodo __toString per le classi di php, in sostanza, se tale classe deve restituire una stringa va tutto bene, ma se deve restiuire un numero si verificano i problemi, per esempio, questo codice restiuisce errore
Codice PHP:
<?php
class tmp{
private $dato;
function __construct($value='') {
$this->dato=$value;
}
function __toString() {
return $this->dato;
}
}
$tmp=new tmp(1);
echo $tmp;
Questo codice invece no:
Codice PHP:
class tmp{
private $dato;
function __construct($value='') {
$this->dato=$value;
}
function __toString() {
return ($this->dato) ? $this->dato : '';
}
}
$tmp=new tmp('1');
echo $tmp;
Ho superato il problema modificando il metodo in questo modo
Codice PHP:
function __toString() {
if(is_numeric($this->dato))
return (string)$this->dato;
return ($this->dato) ? $this->dato : '';
}
ma poi si verifica un Notice se vado a scrivere questo:
$tmp=new tmp(1);
echo $tmp>0;
E' normale come funzionamento?