且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Prestashop 1.7.4-命名空间模块错误

更新时间:2023-11-17 13:07:04

要解决此问题,必须使用自动加载和作曲器.

For solve this problem you must to use autoload and composer.

作曲家:

如果没有,请安装作曲家 https://getcomposer.org/

Install composer if you don't have https://getcomposer.org/

创建composer.json

在模块的文件夹中创建名为composer.json的文件,并插入以下代码

Create inside the module's folder the file named composer.json and insert the below code

{
  "autoload": {
    "psr-4": {
      "Carbo\\": "classes/"
    }
  }
}

在这种情况下, carbo 是我的名字空间, classes 是我将在其中创建课程的文件夹

in this case carbo is my name space and classes is the folder where I will create my classes

使用终端

打开终端并转到模块文件夹,然后用以下命令吃午餐:

open your terminal and go to your module folder and lunch this command:

php composer.phar dump-autoload -a

这将生成一个供应商文件夹,其中包含composer文件夹和autoload.php文件.

This will generate a vendor folder, with inside composer folder and autoload.php file.

在composer文件夹内的autoload_psr4.php中

in autoload_psr4.php inside the composer folder

<?php

// autoload_psr4.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'Carbo\\' => array($baseDir . '/classes'),
);

如何在应用中使用

在以下位置创建您的课程:classes/Helper/Display.php

Create your class in: classes/Helper/Display.php

<?php

namespace Carbo\Helper;

class Display
{
    public static function hello($string){
        return $string;
    }
}

  • 命名空间:Carbo
  • 文件夹:助手
  • 类别名称:显示
  • 打开您的主文件,并在类声明之前包含autoload.php

    open your main file and include autoload.php before the class declaration

require_once __DIR__.'/vendor/autoload.php';

现在您可以加入您的课程

now you can include your classes

use Carbo\Helper\Display; // Namespace - folder - class name

最后使用它

Display::hello("Hello there")

有关此的更多信息,您可以按照本教程进行操作: > https://thewebtier.com/php/psr4-autoloading-php -files-using-composer/

For more about this, you can follow this tutorial: https://thewebtier.com/php/psr4-autoloading-php-files-using-composer/

希望对您有用