首页 > 解决方案 > 致命错误:无法声明 JSMin 类,因为该名称已在 opencart 3.0.3.7 中使用

问题描述

我该如何解决一段代码上的错误,我找不到关于如何解决它的解释,如果有人遇到像我这样的问题,我等你帮助我

 class JSMin {
  const ORD_LF            = 10;
  const ORD_SPACE         = 32;
  const ACTION_KEEP_A     = 1;
  const ACTION_DELETE_A   = 2;
  const ACTION_DELETE_A_B = 3;

  protected $a           = '';
  protected $b           = '';
  protected $input       = '';
  protected $inputIndex  = 0;
  protected $inputLength = 0;
  protected $lookAhead   = null;
  protected $output      = '';

  // -- Public Static Methods --------------------------------------------------

  /**
   * Minify Javascript
   *
   * @uses __construct()
   * @uses min()
   * @param string $js Javascript to be minified
   * @return string
   */
  public static function minify($js) {
    $jsmin = new JSMin($js);
    return $jsmin->min();
  }

标签: phpopencart-3

解决方案


这里有几点注意事项:

  1. 我不使用开放式购物车,所以我不知道你是如何调用这个类的
  2. 您似乎正在尝试使用存档的 git repo rgrove/jsmin-php,位于此处https://github.com/rgrove/jsmin-php

作者已将其存档,并敦促不要再使用它,并建议使用其他更好的解决方案。

可能性一

但是,如果您要继续使用它,您可以使用如下所示的命名空间来修改它——假设 opencart 确实已经在其本机代码中声明了它:

<?php
namespace DeprecatedDontUse;

class JSMin
{
  const ORD_LF            = 10;
  const ORD_SPACE         = 32;
  const ACTION_KEEP_A     = 1;
  const ACTION_DELETE_A   = 2;
  const ACTION_DELETE_A_B = 3;

  protected $a           = '';
  protected $b           = '';
  protected $input       = '';
  protected $inputIndex  = 0;
  protected $inputLength = 0;
  protected $lookAhead   = null;
  protected $output      = '';
  ....etc

然后当你去使用它时,你会这样做:

<?php echo \DeprecatedDontUse\JSMin::minify($JSstring) ?>

那应该解决重复的类命名问题。


可能性二

如果您没有以某种方式将此类自动加载到您的应用程序中,而是手动将其包含在文件中,那么您只需将脚本保持原样(无命名空间),而不是使用require('/path/to/jsmin.php');or include('/path/to/jsmin.php');(无论您使用哪个),您将使用该once版本其中,所以include_once('/path/to/jsmin.php');require_once('/path/to/jsmin.php');

这是为了防止 opencart 在整个执行过程中多次加载您添加的任何脚本。它只会加载一次类,不会给你这个错误。


推荐阅读