当前位置: 首页 > 知识库问答 >
问题:

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

司马庆
2023-03-14

我有一个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');

我明白了

解析错误:语法错误,意外'-

如果我用“publicstatic”声明所有函数,我会得到错误“致命错误:无法在类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';
}
}

共有2个答案

马琛
2023-03-14

在这样的方法声明中,在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,您可以自由地将方法声明为静态。

全昊焜
2023-03-14

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

$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
 类似资料:
  • 我有一个带有下面代码的php文件,我收到错误: 严格标准:非静态方法LinkCore::GetImageLink()不应静态调用,假设$this来自....中的不兼容上下文。 但如果我改变这行: 我得到了 分析错误:语法错误,在/xxx/xxx/publ_html/modules/supplierreports/htmlTemplateCustompdf.php第44行中出现意外的“->”(T_O

  • 我最近对 PHP 5.4 进行了更新,但收到有关静态和非静态代码的错误。 这是错误: 这是第371行: 我希望有人能帮忙。

  • 我正在使用存储库模式并尝试建立模型之间的关系。当我尝试运行存储()方法(在控制器中),该方法试图使用用户()方法(与方模型建立关系)时,我收到以下错误消息: 非静态方法不应该静态调用::user(),假设$this来自不兼容的上下文 我不明白为什么在尝试运行user()relationship方法时会出现此错误,但所有其他方法(包括$this- 以下是相关代码:

  • 我一直试图用我的验证代码进行php pear验证,但我收到的都是严格标准错误--问题是什么?我如何修复它? 电子邮件验证.php

  • PHP严格标准:不应在第33行的/web/sites/blah/somescript.PHP中静态调用非静态方法pear::iserror() 我在MDB2上看到了类似的错误。稍后再详细介绍。 somescript.php: 问题 是否有不同的方法来调用而不会产生错误?

  • 我正在尝试在varGrant CentOS VM上本地运行一个codegniter站点。我已经下载了所有文件,并设置了yaml/host文件等。 我在屏幕上看到这个错误。 遇到 PHP 错误 严重性:8192 消息:非静态方法 MY_Loader::d efine_module() 不应静态调用,假设$this来自不兼容的上下文 文件名:controllers/Front _ controller