1
I have two classes communicating. One of them returns a getter with the month typed in the form, and the other class receives this getter within the cal_days_in_month() function. The problem is that the month field of this function only accepts integer, and as the information comes from a getter it comes as string, would anyone know of any solution?
Follows the class that receives the form data. Receives.php:
<?php
class RecebeDados
{
private $mes, $ano, $feriado, $beneficio, $date;
public function __construct()
{
$this->date = new DateTime('now', new DateTimeZone('America/Sao_Paulo'));
}
public function recebeForm($dados)
{
$this->mes = $dados['mes'];
$this->ano = $dados['ano'];
$this->feriado = $dados['feriado'];
$this->beneficio = $dados['beneficio'];
}
public function getMes()
{
return $this->mes;
}
public function getAno()
{
return $this->ano;
}
public function getFeriado()
{
return $this->feriado;
}
public function getBeneficio()
{
return $this->beneficio;
}
}
$recebe = new RecebeDados();
$recebe->recebeForm($_POST);
require_once 'CalculaBeneficio.php';
$calcBeneficio = new CalculaBeneficio();
$calcBeneficio->calcDias();
$calcBeneficio->calcBeneficio();
The class that uses this data. Calculates.php:
<?php
require_once 'RecebeDados.php';
class CalculaBeneficio extends RecebeDados
{
public $diasUteis, $totalDiasMes, $result, $recebeDados;
public function __construct()
{
$this->recebeDados = new RecebeDados();
}
public function calcDias()
{
$this->diasUteis = 0;
// Obtém o número de dias no mês
$this->totalDiasMes = cal_days_in_month(CAL_GREGORIAN, $this->recebeDados->getMes(), $this->recebeDados->getAno());
for($dia = 1; $dia <= $this->totalDiasMes; $dia++) {
// Verifica os dias úteis do mês (seg a sex)
$timeStamp = mktime(0, 0, 0, $this->recebeDados->getMes(), $this->recebeDados->getDia(), $this->recebeDados->getAno());
$diaSemana = date("N", $timeStamp);
if ($diaSemana < 6) $this->diasUteis++;
}
return $this->diasUteis;
}
public function calcBeneficio()
{
// Faz calculo para retirar os feriados
$this->result = $this->totalDiasMes - $this->recebeDados->getFeriado();
// Calcula o beneficio
return $this->result *= $this->recebeDados->getBeneficio();
header('Location: ../index.php');
}
}
The error you make when I fill in the form is:
Warning: cal_days_in_month(): invalid date.
This is because in the documentation it says that fields should be integer, and they are coming as getters string.
There is another topic regarding converting string to integer: Convert string number to PHP integer
But the solutions in this topic did not solve my problem.
Grateful!
Possible duplicate of Convert string number to PHP integer
– Marcelo de Andrade
I found this topic before publishing it. And as I commented in your reply, the solution presented in the past link did not resolve
– Gabriel
Then it will be good you [Edit] the question and add the error you are giving.
– Woss
But the error is exactly this. But the solution passed on the link did not solve
– Gabriel
@Gabriel this will not work, because the data you fill in
RecebeDados
does not make any relation later with theCalculaBeneficio
(that actually creates a new instance in the constructor).– JuniorNunes
In fact you are using inheritance and composition between two strange classes at least. I don’t see why to use inheritance in this case and the composition seems to be poorly defined.
– Woss
@Gabriel the right one would be you edit the question because your problem is related to the inheritance between your classes.
– JuniorNunes