首页 > 解决方案 > 将变量添加到类名

问题描述

我目前正在为 Wordpress 中的 WPBakery 开发一个元素。

因为我想将元素相互嵌套,所以必须将唯一 ID 附加到元素的类中。变量 $uniqueID 应附加到类“WPBakeryShortCode_vc_BootstrapCol”

但我不能这样做:

if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
        class WPBakeryShortCode_vc_BootstrapCol . $uniqueID extends WPBakeryShortCodesContainer {
    }
}

有人可以给我一个关于如何做到这一点的提示吗?

如果我自己在它后面写 id 它就可以了。像这样:

if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
        class WPBakeryShortCode_vc_BootstrapCol9346f805fc69ff0bb9f83511df30b1a9 extends WPBakeryShortCodesContainer {
    }
}

标签: phpwordpressclassoopwpbakery

解决方案


您可以使用eval()

if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
    eval("class WPBakeryShortCode_vc_BootstrapCol" . (int)$uniqueID . " extends WPBakeryShortCodesContainer {}");
}

虽然这对我来说似乎很奇怪。确保你有充分的理由这样做。


推荐阅读