2014-03-26 68 views
1

我有下面的代碼PHP文件,我收到錯誤:如何解決「非靜態方法XXX :: XXXX()不應該被靜態地稱爲」

嚴格的標準:非靜態方法LinkCore :: getImageLink()不應該被靜態調用,在....

在不兼容的情況下假設$該但是,如果我改變這一行:

$product_image = Link::getImageLink($product->link_rewrite[1], (int)$images[0]['id_image'], 'large_default'); 

$product_image = Link->getImageLink($product->link_rewrite[1], (int)$images[0]['id_image'], 'large_default'); 

我得到

Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in /xxx/xxx/public_html/modules/supplierreports/HTMLTemplateCustomPdf.php on line 44

如果我申報的所有功能與「公共靜態」,我得到錯誤「致命錯誤:無法使非靜態方法HTMLTemplateCore ::的getContent()類HTMLTemplateCustomPdf靜中有」

那麼我能做些什麼來解決這個問題?

<?php 
class HTMLTemplateCustomPdf extends HTMLTemplate 
{ 
public $supplier; 
public function __construct($supplier_order, $smarty) 
{ 
    //print_r($supplier_order); 
      $this->supplier_order = $supplier_order; 
      $this->supplier = new Supplier((int)$this->supplier_order->id_supplier); 
      //$this->supplier_orders = $this->supplier_order->orders 
    $this->smarty = $smarty; 

      // header informations 
      $id_lang = Context::getContext()->language->id; 
      $this->title = HTMLTemplateCustomPdf::l('Supplier ').' : '.$this->supplier->name; 

    // footer informations 
    $this->shop = new Shop(Context::getContext()->shop->id); 
} 
/** 
* Returns the template's HTML content 
* @return string HTML content 
*/ 
public function getContent() 
{ 
      $order_products = array(); 
      $order_customers = array(); 

      if(count($this->supplier_order->orders)){ 
       foreach($this->supplier_order->orders as $order) 
       { 
        //echo $order['id_product']; 

        $product = new Product($order['id_product']); 

        $customer = new Customer((int)$order['id_customer']); 

        $images = Image::getImages(1, (int)$order['id_product']); 

        $order_customers[(int)$order['id_customer']] = array('customer' => $customer); 

        if((int)$images[0]['id_image']) 
        { 
         $product_image = Link::getImageLink($product->link_rewrite[1], (int)$images[0]['id_image'], 'large_default'); 
        } 
        else 
        { 
         $product_image = Link::getImageLink($product->link_rewrite[1], 'en'); 
        } 

        $order_products[(int)$order['id_customer']][] = array('customer' => $customer, 'product' => $product, 'product_quantity' => $order['quantity'], 'product_image' => $product_image); 
        //$order_products[(int)$order['id_customer']][] = array('customer' => '1', 'product' => '2', 'product_quantity' => '3', 'product_image' => '4'); 
       } 
      } 

      //print_r($order_products); 
      //die; 

      //print_r($this->supplier_order->orders); 
      if(count($order_customers) > 0) 
      { 
       $this->smarty->assign(array(
         'suppliers_customers' => $order_customers, 
         'suppliers_products' => $order_products 
       )); 
       return $this->smarty->fetch(_PS_MODULE_DIR_ . 'supplierreports/custom_template_content.tpl'); 
      }else{ 
       return $this->smarty->fetch(_PS_MODULE_DIR_ . 'supplierreports/custom_template_empty.tpl'); 
      } 
} 

public function getFilename() 
{ 
    return 'custom_pdf.pdf'; 
} 
/** 
* Returns the template filename when using bulk rendering 
* @return string filename 
*/ 
public function getBulkFilename() 
{ 
    return 'custom_pdf.pdf'; 
} 
} 

回答

2

你需要創建一個對象來調用類的非靜態方法,

$linkObj = new Link(); 
$product_image = $linkObj->getImageLink($product->link_rewrite[1], (int)$images[0]['id_image'], 'large_default'); 

更多更快的方式,

(new Link)->getImageLink($product->link_rewrite[1], (int)$images[0]['id_image'],  
         'large_default'); // PHP version > 5.4 
0

function之前添加static關鍵字在這樣的方法聲明並按照你的意願靜態調用它。

http://www.php.net/manual/en/language.oop5.static.php

要叫你首先應該實例類new關鍵字實例方法。

如果你使用$this裏面的方法,你肯定會首先獲得類實例。

$link = new Link(); 

$product_image = $link->getImageLink($product->link_rewrite[1], (int)$images[0]['id_image'], 'large_default'); 

如果你不使用$this,您可以自由申報方法是靜態的。

+0

如果該方法在其中引用了'$ this',這將不起作用 –

相關問題