2013-08-23 39 views
0

我在system/helper/wholesaler.php以下輔助函數:

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

我裝在system/startup.php

的問題是助手,當我嘗試使用功能,我收到了致命錯誤「致命錯誤:使用$這個時候不在對象上下文中「。 有沒有辦法在助手中使用$ this?

一種替代的辦法是在is_wholesaler()送$此作爲一個參數或library/customer.php添加功能和與我Opencart的模板視圖文件$this->customer->is_wholesaler()調用它。

回答

0

嘗試創建爲Customerobject,你可以利用它來進行object參考class

$h = new Customer(); 
function is_wholesaler() { 
    return $h->getCustomerGroupId() != 1 ? TRUE : FALSE; 
} 

或者你也可以創建參考

return Customer::getCustomerGroupId() != 1 ? TRUE : FALSE; 
+0

致命錯誤:調用未定義功能get_instance() – Cris

+0

那就試試我的編輯曾經 – Gautam3164

+0

謝謝,但它不工作...未知:非靜態方法客戶:: getCustomerGroupId()不應被稱爲靜態 – Cris

1

$this指的是一個對象(類)的實例,你不能在個人中使用它,你可以pu T功能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; 
} 
相關問題