繁体   English   中英

从另一个类的PHP中的一个类中调用一个函数

[英]calling a function inside a class from another class php

我是php中的新手。 谁能告诉我如何使用功能

    public static function getCategories($id_lang = false, $active = true,$order = true, $sql_filter = '', $sql_sort = '', $sql_limit = '')
            {
             if (!Validate::isBool($active))
             die(Tools::displayError());
             $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
              SELECT *
              FROM `' . _DB_PREFIX_ . 'category` c
              ' . Shop::addSqlAssociation('category', 'c') . '
              LEFT JOIN `' . _DB_PREFIX_ . 'category_lang` cl ON c.`id_category` = cl.`id_category`' . Shop::addSqlRestrictionOnLang('cl') . '
              WHERE 1 ' . $sql_filter . ' ' . ($id_lang ? 'AND `id_lang` = ' . (int)$id_lang : '') . '
              ' . ($active ? 'AND `active` = 1' : '') . '
             ' . (!$id_lang ? 'GROUP BY c.id_category' : '') . '
              ' . ($sql_sort != '' ? $sql_sort : 'ORDER BY c.`level_depth` ASC, category_shop.`position` ASC') . '
            ' . ($sql_limit != '' ? $sql_limit : '')
                      );

                if (!$order)
                   return $result;

                   $categories = array();
                   foreach ($result as $row)
                   $categories[$row['id_parent']][$row['id_category']]['infos'] = $row;

               return $categories;
              }

getCategories()在名为class CategoryCore的类中,我想将此getcategory用作新类totalDiscount ,其中一个名为configure_products();的函数configure_products();

如何在配置产品中使用getcategory()

在页面上包含课程文件

您可以在另一个类中创建该类的对象

function configure_products(){
    $categories = new CategoryCore();
    $categories->getcategory();
   // use $categories to do stuff 
     ......
     .....
}

要么

你可以直接叫

function configure_products(){
     $categories =   CategoryCore::getCategories();
     .....
     ....
}

您的函数getCategories()是静态函数。

因此,可以在类CategoryCore上不创建对象的情况下调用它。

您可以将其用作(使用范围解析运算符):

$categories = CategoryCore::getCategories(YOUR_ARGUMENTS)

参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM