微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP 可恢复的致命错误:方法 Timber\PostType::__toString() 必须返回一个字符串值

如何解决PHP 可恢复的致命错误:方法 Timber\PostType::__toString() 必须返回一个字符串值

我在我的服务器 error_log 文件中遇到了这个错误,由于缺少堆栈跟踪,我似乎无法调试。我收到以下错误

PHP Recoverable Fatal error:  Method Timber\PostType::__toString() must return a string value in ... vendor/twig/twig/src/Environment.PHP(418) : eval()'d code on line 44

我深入研究了库以尝试解决这个问题,但没有这样的运气,因为我不确定 Timber 库的哪些部分调用了这个方法。触发它的代码是,这看起来意味着它应该将 slug 作为字符串返回。

/**
 * Wrapper for the post_type object provided by wordpress
 * @since 1.0.4
*/
class PostType {

    /**
     * @param string $post_type
     */
    public function __construct( $post_type ) {
        $this->slug = $post_type;
        $this->init($post_type);
    }

    public function __toString() {
        return $this->slug;
    }

有谁知道调用函数方法,以及我可以创建堆栈跟踪以解决进一步问题的方法吗? (上面的错误行是我唯一得到的)。

服务器正在运行 PHP 7.4,但很可能也出现在 PHP 7.3 下,因为我最近才升级到 7.4(上个月内),但自今年 1 月以来错误一直在日志中。

解决方法

您分配给属性 $post_type 的参数 $slug 也可能是一个对象。在您的 toString 方法中强制转换为字符串,例如,

public function __toString() {
    return $this->slug.'';
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。