0

に次のヘルパー関数がありますsystem/helper/wholesaler.php

<?php
function is_wholesaler() {
  return $this->customer->getCustomerGroupId() != 1 ? TRUE : FALSE;
}
?>

ヘルパーをロードしましたsystem/startup.php

問題は、関数を使用しようとすると、「致命的なエラー: オブジェクト コンテキストでないときに $this を使用しています」という致命的なエラーが発生することです。ヘルパーで $this を使用する方法はありますか?

別のオプションの 1 つは、$this を引数として送信するis_wholesaler()か、関数を追加して、opencart テンプレート ビュー ファイルでlibrary/customer.php呼び出すことです。$this->customer->is_wholesaler()

4

2 に答える 2

1

$thisオブジェクト (クラス) インスタンスを参照します。個別に使用することはできません。次is_wholesalerのように関数をクラスに入れることができます。

class Helper{
    private $customer;

    public function __construct($customer){
        $this->customer = $customer;
    }

    function is_wholesaler() {
        return $this->customer->getCustomerGroupId() != 1 ? TRUE : FALSE;
    }
}

$customer = new Customer(); //I suppose you have a class named Customer in library/customer.php
$helper = new Helper($customer);
$is_wholesaler = $heler->is_wholesaler();

または、関数 is_wholesaler 自体を次のように変更するだけです。

function is_wholesaler() {
    $customer = new Customer(); //still suppose you have a class named Customer
    return $customer->getCustomerGroupId() != 1 ? TRUE : FALSE;
}
于 2013-08-23T05:58:58.480 に答える