如何解决“非静态方法xxx::xxxx()不应该静态调用”的问题?


How can I solve "Non-static method xxx::xxxx() should not be called statically"

我有一个php文件,下面的代码,我收到错误:

严格标准:非静态方法LinkCore::getImageLink()不应该静态调用,假设$this来自....

中不兼容的上下文

但是如果我改变这一行:

$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');

解析错误:语法错误,意外'->' (T_OBJECT_OPERATOR)在/xxx/xxx/public_html/modules/supplierreports/HTMLTemplateCustomPdf.php第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';
}
}

您需要创建一个对象来调用该类的非静态方法

$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

在此方法声明中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,可以自由地将method声明为static。