Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
porque desse erro ? :P primeira vez que uso o __toString algo errado no uso ?
<?php
class MonthChange{
private $ArrayMonth;
private $Month;
public function __construct($Month){
$this->ArrayMonth =
array(1 => 'Janeiro',
2 => 'Fevereiro',
3 => 'Março',
4 => 'Abril',
5 => 'Maio',
6 => 'Junho',
7 => 'Julho',
8 => 'Agosto',
9 => 'Setembro',
10 => 'Outubro',
11 => 'Novembro',
12 => 'Dezembro'
);
$this->Month = $Month;
}
public function __toString(){
return $this->ArrayMonth[$this->Month];
}
}
echo New MonthChange(4);
> Catchable fatal error: Method MonthChange::__toString() must return a string value
valww
é estranho
tem que debugar isso ai
mas o que pode ta acontecendo é que o tostring ta tentando retornar aquele valor antes do construct setar
>
mas o que pode ta acontecendo é que o tostring ta tentando retornar aquele valor antes do construct setar
eh uma hipotese .. pois a principio o valor que retorna é uma string, mas o construct não devia se o primeiro a ser carregado ?
testes
$a = New MonthChange(4);
var_dump($a->Return);
string(5) "Abril"
<?php
class MonthChange{
private $ArrayMonth;
private $Month;
public $Return;
public function __construct($Month){
$this->ArrayMonth =
array(1 => 'Janeiro',
2 => 'Fevereiro',
3 => 'Março',
4 => 'Abril',
5 => 'Maio',
6 => 'Junho',
7 => 'Julho',
8 => 'Agosto',
9 => 'Setembro',
10 => 'Outubro',
11 => 'Novembro',
12 => 'Dezembro'
);
$this->Month = $Month;
$this->Return = $this->ArrayMonth[$this->Month];
}
public function __toString(){
//return $this->ArrayMonth[$this->Month];
}
}Will,
O erro está em aceitar qualquer coisa como válido, se, por exemplo, seu construtor receber um valor X tal que 0 >= X ou X >= 13 ou X não for um número, seu código irá falhar.
Valide sempre, qualquer informação que chegar para você:
final class Months {
const JANEIRO = 1;
const FEVEREIRO = 2;
const MARCO = 3;
const ABRIL = 4;
const MAIO = 5;
const JUNHO = 6;
const JULHO = 7;
const AGOSTO = 8;
const SETEMBRO = 9;
const OUTUBRO = 10;
const NOVEMBRO = 11;
const DEZEMBRO = 12;
private function __construct(){}
}
class MonthChange{
private $ArrayMonth;
private $Month;
public function __construct($Month = Months::JANEIRO ){
$this->ArrayMonth = array(
Months::JANEIRO => 'Janeiro',
Months::FEVEREIRO => 'Fevereiro',
Months::MARCO => 'Março',
Months::ABRIL => 'Abril',
Months::MAIO => 'Maio',
Months::JUNHO => 'Junho',
Months::JULHO => 'Julho',
Months::AGOSTO => 'Agosto',
Months::SETEMBRO => 'Setembro',
Months::OUTUBRO => 'Outubro',
Months::NOVEMBRO => 'Novembro',
Months::DEZEMBRO => 'Dezembro'
);
$this->setMonth( $Month );
}
public function __toString(){
return $this->ArrayMonth[ $this->Month ];
}
public function setMonth( $Month ){
if ( is_numeric( $Month ) ){
switch ( $Month ){
case Months::JANEIRO:
case Months::FEVEREIRO:
case Months::MARCO:
case Months::ABRIL:
case Months::MAIO:
case Months::JUNHO:
case Months::JULHO:
case Months::AGOSTO:
case Months::SETEMBRO:
case Months::OUTUBRO:
case Months::NOVEMBRO:
case Months::DEZEMBRO:
$this->Month = $Month;
break;
default:
throw new UnexpectedValueException( 'O valor de $Month é inválido.' );
}
} else {
throw new InvalidArgumentException( '$Month deve ser um valor numérico.' );
}
}
}
Dessa forma, você terá a garantia de que tudo funcionará como esperado e, caso um valor inválido chegue, seu código o rejeite:
echo new MonthChange( 4 ); //Abril
echo new MonthChange( Months::JANEIRO ); //Janeiro
Agora, garantindo que nada falhe:
try {
echo New MonthChange( 127 );
} catch ( UnexpectedValueException $e ){
echo $e->getMessage() , '<br />';
echo 'Precisamos de um valor entre 1 e 12 inclusive para ser o mês.<br />';echo $e->getMessage() , '<br />';
echo 'Talvez você tenha esquecido de informar o mês, deseja assumir o padrão como Janeiro ?<br />';
}> Catchable fatal error: Method MonthChange::__toString() must return a string value
:o I AM NINJA
ahshdha capzz ta funcionando certo( na verdade acho que deveria exibir o erro não ?)..
mas achei o meu erro ..
var_dump(date("m"));
echo new MonthChange(date("m"));
>
string(2) "04"
Catchable fatal error: Method MonthChange::__toString() must return a string value
valww galera
http://br2.php.net/manual/pt_BR/language.oop5.references.php
<?php
class MonthChange{
private $ArrayMonth = array(
'Mês inválido',
'Janeiro',
'Fevereiro',
'Março',
'Abril',
'Maio',
'Junho',
'Julho',
'Agosto',
'Setembro',
'Outubro',
'Novembro',
'Dezembro'
);
private $Month;
public function __construct($Month){
if(!isset($this->ArrayMonth[$Month])) $Month = 0;
$this->Month = $Month;
}
public function __toString(){
$ret = $this->ArrayMonth[$this->Month];
return $ret;
}
}
$Janeiro = new MonthChange(3);
echo $Janeiro;
?>
>
porque desse erro ?
O método mágico __toString() precisa necessariamente retornar uma string e não pode, sob hipótese alguma, disparar uma exceção.
Reveja o conteúdo que você está tentando retornar, se não for uma string, não vai funcionar.