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

核心文件之一 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

我错过了什么?这是扩展类和调用方法的正确方法吗?


猛跑小猪
浏览 138回答 2
2回答

慕村9548890

ObjectModel.php 文件<?php&nbsp; &nbsp; class ObjectModel{&nbsp; &nbsp; &nbsp; &nbsp; //For example I created non-static function in ObjectModel class&nbsp; &nbsp; &nbsp; &nbsp; public function getStockAvailableIdByProductId($id){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "test";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //For example I created static function in ObjectModel class&nbsp; &nbsp; &nbsp; &nbsp; public static function getStockAvailableIdByProductIdStatic($id){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "teststatic";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;?>StockAvailable.php 文件。<?php//Extends used to inherit the parent class property&nbsp; &nbsp; class StockAvailableCore extends ObjectModel&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;public static function getQuantityAvailableByProduct($id_product = null, $id_product_attribute = null, $id_shop = null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp; ?>运行.php文件<?phprequire_once('ObjectModel.php');require_once('StockAvailable.php');$MyClass = new StockAvailableCore();// Access the ObjectModel function//to access the&nbsp; Non-static method need to create the object.echo $MyClass->getStockAvailableIdByProductId($id);//Static method access by class reference (::)echo StockAvailableCore::getStockAvailableIdByProductIdStatic($id);?>
打开App,查看更多内容
随时随地看视频慕课网APP