声明PHP类的其他方法



我正在Wooccommerce中编写一个复杂的产品类型,我不喜欢大多数WordPress代码的外观(默认情况下有点乱,而且"不诗意"(,所以我决定使用PHP命名空间和自动加载。所以我必须在WordPress可以获取的范围内声明我的产品类型类。以下是事实上的做法。

add_action('init', function(){
class WC_Product_Partner_Aware extends WC_Product_Variable{
// Content here is very large and seems very awkward to be in a file call function.php that contains other actions and filters
}
});

我想在一个有App/ProductType/的命名空间中声明这个类。我已经尝试了所有的东西,但都不起作用。出于沮丧,我也尝试了以下代码

add_action('init', function(){
define('WC_Product_Partner_Aware', 'App/ProductType/PartnerAwareProduct');
});

有人能帮忙吗?我想成为一名代码诗人,但WordPress让这一节不押韵。

您可以在init函数之外分离类。只要确保在运行init函数时它是可用的。

add_action('init', function(){
// you can call class with namespace if desired
if(class_exists(somenamespaceWC_Product_Partner_Aware)){
$ppa = new somenamespaceWC_Product_Partner_Aware();
$ppa->doStuff();
}
});

WC_Product_Partner_Awre.php

namespace somenamespace;
class WC_Product_Partner_Aware extends WC_Product_Variable{
//your properties and methods.
} 

相关内容

最新更新