Turn string into integer in PHP

Asked

Viewed 1,288 times

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!

  • 3

    Possible duplicate of Convert string number to PHP integer

  • I found this topic before publishing it. And as I commented in your reply, the solution presented in the past link did not resolve

  • Then it will be good you [Edit] the question and add the error you are giving.

  • But the error is exactly this. But the solution passed on the link did not solve

  • 1

    @Gabriel this will not work, because the data you fill in RecebeDados does not make any relation later with the CalculaBeneficio (that actually creates a new instance in the constructor).

  • 1

    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.

  • @Gabriel the right one would be you edit the question because your problem is related to the inheritance between your classes.

Show 2 more comments

2 answers

2

You can do it by making a casting using the modifiers int or integer:

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

Or using the function intval:

$string = '100';
$int = intval($string); 
  • Hello, thanks for the feedback. These two options I had already tried to realize but it did not work, continued presenting the same problem. Had placed intval($this->receivedDados->getMes()) and (int)$this->receivedDados->getMes(). And neither of them solved the problem, so I tried to convert it directly into the variable before calling the getters, so (int)$this->receivedDados, or so intval($this->receivedDados). And it didn’t solve :/

  • Gives a var_dump in those outputs there, there is something beyond the conversion in that.

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();

In the construct class CalculaBeneficio:

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

In class CalculaBeneficio where you have $this->recebeDados you delete the received, example:

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

In the class builder 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.

  • I tried this way but the error keeps popping up when I fill in the form. The error you present is: Warning: cal_days_in_month(): invalid date.

  • @Gabriel Check the data coming in $this->getMes() and in the $this->getAno() compares with the syntax of call_days_in_month

  • Is returning null

  • @Gabriel sees what’s coming inside the $ __construct

  • @Gabriel sorry, had forgotten the class Received, I edited the answer, you will need to touch the __construct class RecebeDados.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.