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
I think it should be possible to pass it as an argument to the function
main
.– stderr
You can demonstrate how I would do that @qmechanik .... Thank you.
– Marcos Vinicius
or it may be a global variable, but I think it is much better to pass as a parameter of
main()
also– Erlon Charles