自动加载在Laravel中包含多个类的Php文件

自动加载在Laravel中包含多个类的Php文件

本文介绍了自动加载在Laravel中包含多个类的Php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.请我在这个问题上陷入困境,我需要帮助.我有这个php文件,其中包含多个类.我想自动加载这些类,以便laravel可以执行它们.我该怎么办

Good day guys. Please I am stucked on this problem and i want help. I have this php file that i have included multiple classes. I want to autoload those classes so that laravel can execute them. How do i go about it

以下代码可在app/mpower/mpower.php中找到,所需文件位于mpower目录中嵌套的其他目录中.

The codes below can be found in app/mpower/mpower.php and the required files are in other directories nested in the mpower directory.

我如何自动加载这些类,以便可以使用其他后续文件中的所有类

how do i autoload these classes so that i can use all the classes in the other subsequent files

<?php
require_once("mpower/dependency_check.php");

set_include_path(get_include_path() . PATH_SEPARATOR . realpath(dirname(__FILE__)));

abstract class MPower {
  const VERSION = "1.2.0";
  const SUCCESS = "success";
  const FAIL = "fail";
  const PENDING = "pending";
}

if (strnatcmp(phpversion(),'5.3.0') >= 0) {
  define('JSON_ENCODE_PARAM_SUPPORT',   true);
}else{
  define('JSON_ENCODE_PARAM_SUPPORT',   false);
}

///define("MPOWER_SUCCESS", "success");
//define("MPOWER_FAIL", "fail");
//define("MPOWER_PENDING", "pending");

require_once("mpower/setup.php");
require_once("mpower/customdata.php");
require_once("mpower/checkout.php");
require_once("mpower/checkout/store.php");
require_once("mpower/checkout/checkout_invoice.php");
require_once("mpower/checkout/onsite_invoice.php");
require_once("mpower/direct_pay.php");
require_once("mpower/direct_card.php");
require_once("mpower/libraries/Requests.php");
require_once("mpower/utilities.php");

推荐答案

您可以通过自动加载"部分中的composer.json自动加载类

You can autoload your class through composer.json in the "autoload" section

"autoload": {
    "classmap": [
        "database",
        "app/mpower"
    ],
    "files": [
        "app/mpower/mpower.php"
    ],
},

我展示了两种不同的方式.请使用文件加载文件或使用classmap加载目录.不是都.

I showed 2 different ways. Please use either files, to load the file, or classmap, to load the directory. Not both.

这篇关于自动加载在Laravel中包含多个类的Php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:34