通过函数传递对象

时间:2014-11-18 14:41:20

标签: php function object parameters

我试图通过函数传递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,
        ));
    }
}

1 个答案:

答案 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;