Transform string into integer in PHP

1

I have two classes communicating. One of these returns a getter with the month entered in the form, and the other class receives that getter inside the cal_days_in_month () function. The problem is that the month field of this function only accepts integer, and since the information comes from a getter it comes as string, would anyone know of any solution?

The class that receives the data from the form follows. Receb.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. Calculate.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 that occurs when I fill out the form is:

Warning: cal_days_in_month(): invalid date. 

This is because in the documentation it says that the fields must be integers, and they are coming as getters string.

There is another topic regarding converting string to integer: Convert string number to integer PHP

But the solutions in this topic did not solve my problem.

Thank you!

    
asked by anonymous 19.05.2017 / 20:40

2 answers

2

You can do this by doing a casting using the int or integer modifiers:

$string = '100';
$int = (int) $string;

Or using intval function:

$string = '100';
$int = intval($string); 
    
19.05.2017 / 20:49
0

You'll need to change your code a little bit:

To make it work use these lines:

require_once 'CalculaBeneficio.php';

$calcBeneficio = new CalculaBeneficio($_POST);
$calcBeneficio->calcDias();
$calcBeneficio->calcBeneficio();

No construct of class CalculaBeneficio :

public function __construct($data)
{
    parent::__construct($data);
}

In class CalculaBeneficio where you have $this->recebeDados you delete the receivedData, eg:

$this->recebeDados->getMes(); // Antigo
$this->getMes(); // Novo

In the constructor of class RecebeDados :

public function __construct($data)
{
    $this->date = new DateTime('now', new DateTimeZone('America/Sao_Paulo'));
    $this->mes = $data['mes'];
    $this->ano = $data['ano'];
    $this->feriado = $data['feriado'];
    $this->beneficio = $data['beneficio'];
}

Here you can read more about inheritance in PHP .

    
19.05.2017 / 21:12