簡體   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