(PrestaShop)从核心类调用静态方法的正确方法

时间:2019-05-14 17:18:09

标签: php prestashop prestashop-1.7

其中一个核心文件 classes / stock / StockAvailable.php 包含:

class StockAvailableCore extends ObjectModel
{
   public static function getQuantityAvailableByProduct($id_product = null, $id_product_attribute = null, $id_shop = null)
     {
       ...
     }
  ...
}

我想从我的模块中调用此 getQuantityAvailableByProduct()方法。

因此,我尝试包括此类(它是父亲类和父亲的接口),对其进行扩展并调用如下方法:

require_once('../../src/Core/Foundation/Database/EntityInterface.php');
require_once('../../classes/ObjectModel.php');
require_once('../../classes/stock/StockAvailable.php');

$MyClass = new StockAvailableCore();

$MyClass->getStockAvailableIdByProductId($id);

我得到的错误:

PHP Fatal error:  Uncaught Error: Class 'ObjectModel' not found in /home/mantas/Server/honey/classes/stock/StockAvailable.php:34

我想念什么?这是扩展类和调用方法的正确方法吗?

2 个答案:

答案 0 :(得分:0)

ObjectModel.php文件

<?php

    class ObjectModel{

        //For example I created non-static function in ObjectModel class
        public function getStockAvailableIdByProductId($id){
            return "test";
        }

        //For example I created static function in ObjectModel class
        public static function getStockAvailableIdByProductIdStatic($id){
            return "teststatic";
        }

    }
   ?>

StockAvailable.php文件。

<?php
//Extends used to inherit the parent class property
    class StockAvailableCore extends ObjectModel
    {
       public static function getQuantityAvailableByProduct($id_product = null, $id_product_attribute = null, $id_shop = null)
         {

         }

    }


    ?>

run.php文件

<?php

require_once('ObjectModel.php');
require_once('StockAvailable.php');

$MyClass = new StockAvailableCore();

// Access the ObjectModel function

//to access the  Non-static method need to create the object.
echo $MyClass->getStockAvailableIdByProductId($id);

//Static method access by class reference (::)
echo StockAvailableCore::getStockAvailableIdByProductIdStatic($id);
?>

答案 1 :(得分:0)

您可以通过StockAvailable::getQuantityAvailableByProduct($id_product, $id_product_attribute)来称呼它。而且,如果您根据Documentation制作模块,则甚至无需在代码开头包含任何文件