質問

サードパーティの小売業者とAPI統合を作成しました。私は自分で作成しました Mage::dispatchEvent 通信の前後に電話をかけます。私はこれらを主にロギング/デバッグモードに使用しますが、このAPIを使用する別の、ゆるく関連するモジュールからのデータを操作するためにそれらを使用する機会があります。

以下の例では、操作することができます $post_string 私のオブザーバーが何をするかに基づいたこのローカル範囲で?

Mage::dispatchEvent('my_api_delete_before', array('post_string'=>$post_string, 'my_cart'=>$this));

$result = $this->doApiCommunication($post_string);

Mage::dispatchEvent('my_api_delete_after', array('post_string'=>$post_string, 'result'=>$result, 'product'=>$product, 'cart_item_id'=>$_item->cartItemId));
役に立ちましたか?

解決

もしも $post_string 文字列です。文字列は関数を参照して渡されないため、オブザーバークラスで行われた変更はこの範囲に表示されません。

そのためのいくつかの解決策があります:

迅速ですが推奨されていません:

PHPは、関数を参照して文字列を強制的に渡すことにより、回避策を提供します。 & 変数名の前に、そう:

Mage::dispatchEvent('my_api_delete_before', array('post_string'=>&$post_string, 'my_cart'=>$this));

しかし、これが推奨されない理由 @dedmeet 彼で言った コメント:

PHPバージョン5.4.0以降、コールタイムのパスごとの参照を使用することはサポートされていません

から直接 PHPマニュアル:

ノート: 関数呼び出しには、関数定義にのみ参照サインがありません。関数の定義だけで、参照によって引数を正しく渡すのに十分です。 PHP 5.3.0の時点で、「コールタイムパスバイリファレンス」が使用されたときに廃止されるという警告が表示されます。また、PHP 5.4.0の時点で、コールタイムパスごとの参照が削除されたため、使用すると致命的なエラーが発生します。

だからここにそれを行う方法があります

クリーナーと推奨:

より良い解決策は、aを作成することです Varien_Object クラスは常に参照によって渡されるためです。拡張されるクラス Varien_Object そしてその Varien_Object それ自体で、マネントの周りにあるゲッターとセッターを使用することができます。

$obj = new Varien_Object();

// Set a value for data variable
$obj->setFoo($bar);

// The previous function is the same as the following:
// In fact, the previous function will call setData with 'foo' and $bar as its paramteres
$obj->setData('foo', $bar);

// To get the value of 'foo' you will need to call any of the following functions:
$obj->getFoo();

$obj->getData('foo');    

したがって、実装する Varien_object OPの例では:

class Foo_Bar_Model_Communicator{

    public function someFunction(){
        /*
         * Assuming that $post_string is getting set here somehow
         * before the next line.
         */
        $eventData = new Varien_Data();

        $eventData->setData('post_string', $post_string);   
        $eventData->setData('cart', $this); 

        Mage::dispatchEvent('my_api_delete_before', array('event_data'=>$eventData));

        /* In this case, since we sent a Varien_Object as parameter,
         * whatever the Observer class does to the object, will be reflected
         * here as well.
         * We only need to make sure that we get the string back from the
         * Varien_Object instead of using $post_string.
         */
        $new_post_string = $eventData->getData('post_string');

        $result = $this->doApiCommunication($new_post_string);

        /*
         * We can do the same for the parameters for the next event, but it
         * wasn't part of the OP's question and it won't be needed since
         * there is no code following the dispatch event.
         *
         * However it would be a better idea to make the "before" & "after"
         * events have the same type of parameters which would be less 
         * confusing later on.
         */

        Mage::dispatchEvent('my_api_delete_after', array('post_string'=>$post_string, 'result'=>$result, 'product'=>$product, 'cart_item_id'=>$_item->cartItemId));
    }
}
ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top