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

问题描述

我目前正在尝试使用 html 内容内联 css,该内容完美但只有一次。当我多次尝试时,我收到此错误:

PHP 致命错误:无法声明类 Emogrifier,因为该名称已在使用中

在我的 Wordpress 子主题 functions.php 文件中包含的 email-functions.php 中引发了错误:

/**
 * Include email functions for ultimate member
 */
require 'ultimate-member/functions/email-functions.php';

这是导致问题的 email-functions.php 文件中的函数:

/**
 * Apply css to UM email message from template
 */
add_filter( 'um_email_send_message_content', 'apply_style_to_email', 10, 99);
function apply_style_to_email($message, $slug, $args ) {

    //Get Emogrifier class
    include_once get_home_path() . 'wp-content/themes/DiviChild/ultimate-member/libraries/class-emogrifier.php';

    ob_start();
    include_once get_home_path() . 'wp-content/themes/DiviChild/woocommerce/emails/email-styles.php';
    $css = apply_filters( 'woocommerce_email_styles', ob_get_clean() );

    // apply CSS styles inline for picky email clients.
    try {
        $emogrifier = new Emogrifier( $message, $css );
        $message    = $emogrifier->emogrify();
    } catch ( Exception $e ) {
        $logger = wc_get_logger();
        $logger->error( $e->getMessage(), array( 'source' => 'emogrifier' ) );
    }

    return $message;
}

如您所见,我正在使用 include_once 来获取 Emogrifier 类。我真的不知道我在这里做错了什么。

这是 Emogrifier 类:https ://codeshare.io/50B0ml

标签: phpwordpresswoocommerce

解决方案


该错误很好地描述了您的问题。看起来您的项目中有 2 个同名的类。在这里,NameSpace 可以帮助您。

最好在你的项目文件中搜索关键字“emogrifier”,也许你在项目中有 2 次类。或者该类通过 Composer 来自供应商目录之一,但 Composer Packages NameSpaces 不应该发生这种情况。

http://php.net/manual/de/language.namespaces.php


推荐阅读