php - 将变量添加到类名
问题描述
我目前正在为 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 {
}
}
解决方案
您可以使用eval()
if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
eval("class WPBakeryShortCode_vc_BootstrapCol" . (int)$uniqueID . " extends WPBakeryShortCodesContainer {}");
}
虽然这对我来说似乎很奇怪。确保你有充分的理由这样做。
推荐阅读
- python - 我在同一脚本中运行 tensorflow 单次检测器和 Keras convnet 时遇到问题。收到这个奇怪的错误?
- c# - C# - 为什么我不能创建一个泛型接口类型的 IEnumerable?
- powershell - 使用 PowerShell 来自 Google 表格的 DL CSV
- ruby-on-rails - 每天只获取一条记录
- amazon-web-services - 使用 AWS CLI 'describe-instances' 从两个级别检索元素
- java - Testdome - DecoratorStream Java
- r - 从事务行在 R 中创建矩阵
- angular - 无法访问 Firestore 集合 ID
- javascript - 消费 Kafka 主题后发送 HTTP 响应
- canvas - 无法使用 Konva 将 PDF 图像作为图像对象加载到 HTML5 画布中