How to import a variable within a function of a class?

Asked

Viewed 3,697 times

9

I’d like to take the $valortotal and take it into a function of a class as can be seen in the example below. Thank you.

$valortotal = 15.00;

class CreatePaymentRequestLightbox
{
    public static function main()
    {
        $pedido_total = $valortotal;
    ....
.....
  • 2

    I think it should be possible to pass it as an argument to the function main.

  • You can demonstrate how I would do that @qmechanik .... Thank you.

  • or it may be a global variable, but I think it is much better to pass as a parameter of main() also

5 answers

8


One possibility would be to pass the variable as an argument to the function main.

class CreatePaymentRequestLightbox{
    public function main($valortotal){
        $pedido_total = $valortotal;
        echo $pedido_total;
    }
}

$valortotal = 15.00;
$paymentRequest = new CreatePaymentRequestLightbox();
$paymentRequest->main($valortotal);

See demonstração

Or use a class public variable:

class CreatePaymentRequestLightbox{
    public $pedido_total = 0;

    public function main(){
        echo $this->pedido_total;
    }
}

$valortotal = 15.00;
$paymentRequest = new CreatePaymentRequestLightbox();
$paymentRequest->pedido_total = $valortotal;
$paymentRequest->main();

See demonstração

Or use a private variable and change the value with a public function:

class CreatePaymentRequestLightbox{
    private $pedido_total = 0;

    public function editarPedidoTotal($pedido_total){
        $this->pedido_total = $pedido_total;
    }
    public function main(){
        echo $this->pedido_total;
    }
}

$valortotal = 15.00;
$paymentRequest = new CreatePaymentRequestLightbox();
$paymentRequest->editarPedidoTotal($valortotal);
$paymentRequest->main();

See demonstração

4

Do main() accept a parameter.

$valortotal = 15.00;

class CreatePaymentRequestLightbox
    {

        public static function main($valortotal)
             {

                  $pedido_total = $valortotal;

This way you pass the direct value or variable when calling the function

$valortotal = 15.00;
$paymentRequest = new CreatePaymentRequestLightbox()->main($valortotal);

4

If the value is already within the class

class CreatePaymentRequestLightbox
    {

private $valorTotal = '150.00';

        public static function main()
             {

                  $pedido_total = $this->valorTotal;

In case you go get out of class to play in

class CreatePaymentRequestLightbox
    {

public $valorTotal;

        public static function main()
             {

                  $pedido_total = $this->valorTotal;
}
}

$classe = new CreatePaymentRequestLightbox;
$classe->valorTotal = '150.00';

1

Only complementing the other answers and also exemplifying the comment from @Erloncharles, which is not recommended for all cases (as already mentioned) and almost always the best option is to pass the value per parameter.

But in some cases, as a connection object, it is best to use a global variable:

$connection = new PDO();

class AlgumaClasse
{
    public static function main($valortotal)
    {
        global $connection;
        $connection->query("INSERT INTO valores (valor) VALUES ({$valortotal})");
    }
}

AlgumaClasse::main(15.00);
  • Global variable is a good idea !!! :)

1

It’s not recommended, but you could use it like this too:

$valortotal = 15.00;

class CreatePaymentRequestLightbox
{
    public static function main()
    {
        global $valortotal;
        $pedido_total = $valortotal;
    ...
...

Browser other questions tagged

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