我试图通过函数传递PHP对象作为参数,但是我得到以下错误:
注意:尝试获取非对象的属性
在非对象
上调用成员函数create()$mollie = new Mollie_API_Client;
$mollie->setApiKey("0000");
add_action( 'init', 'gtp_mollie_payment_submit' );
function gtp_mollie_payment_submit( $mollie ) {
if( isset( $_POST['checkout_submit'] ) ) {
$payment = $mollie->payments->create(array(
"amount" => 10.00,
"description" => "My first API payment",
"redirectUrl" => "https://webshop.example.org/order/12345/",
"method" => Mollie_API_Object_Method::IDEAL,
));
}
}
答案 0 :(得分:1)
我用面向对象的方式解决了它:
class MyMollieGateway {
private $mollie;
function __construct() {
$this->mollie = new Mollie_API_Client;
$this->mollie->setApiKey( "0000" );
add_action( "init", array( $this, "createPayment" ) );
}
function createPayment() {
if( isset( $_POST['checkout_submit'] ) ) {
$payment = $mollie->payments->create(array(
"amount" => 10.00,
"description" => "My first API payment",
"redirectUrl" => "https://webshop.example.org/order/12345/",
"method" => Mollie_API_Object_Method::IDEAL,
));
}
}
}
$_mollie = new MyMollieGateway;