如何解决"非静态方法xxx :: xxxx()不应该静态调用"

use*_*465 2 php static-methods

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

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

但如果我改变这一行:

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

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

我明白了

第44行的/xxx/xxx/public_html/modules/supplierreports/HTMLTemplateCustomPdf.php中的解析错误:语法错误,意外的' - >'(T_OBJECT_OPERATOR)

如果我用"public static"声明所有函数,我会在类HTMLTemplateCustomPdf中得到错误"致命错误:无法使非静态方法HTMLTemplateCore :: getContent()静态"

那么我该怎么做才能解决这个问题呢?

<?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';
}
}
Run Code Online (Sandbox Code Playgroud)

Rik*_*esh 5

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

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

更快的方式,

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