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

9

I would like to get 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;
    ....
.....
    
asked by anonymous 19.05.2015 / 22:10

5 answers

8

One possibility would be to pass the variable as 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 public variable of class:

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

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

View demonstração

    
19.05.2015 / 22:21
4

Make main() accept a parameter.

$valortotal = 15.00;

class CreatePaymentRequestLightbox
    {

        public static function main($valortotal)
             {

                  $pedido_total = $valortotal;

In this way you pass the value directly or the variable when you call the function

$valortotal = 15.00;
$paymentRequest = new CreatePaymentRequestLightbox()->main($valortotal);
    
19.05.2015 / 22:15
4

If the value is already within the class

class CreatePaymentRequestLightbox
    {

private $valorTotal = '150.00';

        public static function main()
             {

                  $pedido_total = $this->valorTotal;

If you go get outside the class to play in

class CreatePaymentRequestLightbox
    {

public $valorTotal;

        public static function main()
             {

                  $pedido_total = $this->valorTotal;
}
}

$classe = new CreatePaymentRequestLightbox;
$classe->valorTotal = '150.00';
    
19.05.2015 / 22:42
1

Just complementing the other answers and also exemplifying the #, 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);
    
10.11.2015 / 19:46
1

Not recommended, but you could use it as well:

$valortotal = 15.00;

class CreatePaymentRequestLightbox
{
    public static function main()
    {
        global $valortotal;
        $pedido_total = $valortotal;
    ...
...
    
04.05.2017 / 21:50