我知道有一些關於它的線程,但似乎沒有任何工作......我試圖在PHP中創建類似全局類變量的東西,因此理論上我想要類似這樣的東西:PHP類變量
(PseudoCode後面):
class GlobalVarClass{
globalVar $price;
function doSomethingWithThePrice(){
//get some Values, etc..
$this->price = $xyz;
}
function needThePriceNow(){
//this is where the price is needed now, so something like
echo $this->price;
}
}
我該怎麼做?順便說一句:
我使用Laravel順便說一句,我不能設置在構造函數中這個值,因爲我有它尚不可用......,如果這使事情更容易...
編輯: 確切的說,這是我的代碼:
class VoucherController extends Controller
{
private $_apiContext;
protected $voucherValue;
public function __construct()
{
$this->_apiContext = PayPal::ApiContext(
config('services.paypal.client_id'),
config('services.paypal.secret'));
$this->_apiContext->setConfig(array(
'mode' => 'sandbox',
'service.EndPoint' => 'https://api.sandbox.paypal.com',
'http.ConnectionTimeOut' => 30,
'log.LogEnabled' => true,
'log.FileName' => storage_path('logs/paypal.log'),
'log.LogLevel' => 'FINE'
));
}
public function index(){
return view('voucher.createVoucher');
}
public function payVoucher(Request $request){
$userInfo = \App\User::where('id','=',\Auth::user()->id)
->first();
if($userInfo->street == ""){
$request->session()->flash('alert-info', 'Vor der ersten Bestellung müssen Sie erst Ihre Daten vervollständigen');
return redirect('/editData');
}
$itemList = PayPal::itemList();
$payPalItem = PayPal::item();
$payPalItem->setName('Gutschein')->setDescription('Gutschein für Shopping Portal')->setCurrency('EUR')->setQuantity(1)->setPrice($request->input('voucherValue'));
$total = $request->input('voucherValue');
$this->voucherValue = $total;
$itemList->addItem($payPalItem);
$payer = PayPal::Payer();
$payer->setPaymentMethod('paypal');
$details = PayPal::details();
$details->setShipping("0")
->setSubtotal($total);
$amount = PayPal:: Amount();
$amount->setCurrency('EUR');
$amount->setTotal(($total));
$amount->setDetails($details);
$transaction = PayPal::Transaction();
$transaction->setAmount($amount);
$transaction->setItemList($itemList);
$transaction->setDescription('Ihr Gutschein');
$redirectUrls = PayPal:: RedirectUrls();
$redirectUrls->setReturnUrl(action('[email protected]'));
$redirectUrls->setCancelUrl(action('[email protected]'));
$payment = PayPal::Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$payment->setRedirectUrls($redirectUrls);
$payment->setTransactions(array($transaction));
$response = $payment->create($this->_apiContext);
$redirectUrl = $response->links[1]->href;
return Redirect::to($redirectUrl);
}
public function getDone(Request $request)
{
$id = $request->get('paymentId');
$token = $request->get('token');
$payer_id = $request->get('PayerID');
$payment = PayPal::getById($id, $this->_apiContext);
$paymentExecution = PayPal::PaymentExecution();
$paymentExecution->setPayerId($payer_id);
$executePayment = $payment->execute($paymentExecution, $this->_apiContext);
// Clear the shopping cart, write to database, send notifications, etc.
// Thank the user for the purchase
//Gutscheincode generieren, in DB eintragen, und per Mail verschicken
$voucherCode = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 12);
$voucher = new Voucher;
$voucher->voucherValue = $this->voucherValue;
$voucher->voucherCode = $voucherCode;
$voucher->save();
return view('voucher.buyDone', ['voucherCode' => $voucherCode, 'payment' => $payment]);
}
所以我做了支付寶付款。在函數payVoucher
中,我從request
獲得了憑證值(這是應該可以在其他函數中訪問的變量),然後在函數getDone
中,我需要訪問它才能將其輸入到數據庫中。我怎樣才能做到這一點?
爲downvoted的一個:感謝不告訴我爲什麼至少;) 問題是,變量似乎一直保持undefined,這是一個對象var,所以我嘗試使用其中之一,但它是未定義的當我嘗試在第二個函數 – nameless
@jeroen中使用它時,我編輯了我的問題以使用我的真實代碼,只是認爲它可能不會造成混淆,因爲「PayPal」不是問題所在。 – nameless
這是不是相同請求,還是在例如重定向之後?你的代碼似乎建議重定向;在這種情況下,您需要更多的持久性存儲,如會話或數據庫。 – jeroen