声明PHP类的其他方法?

声明PHP类的其他方法?,php,wordpress,class,woocommerce,Php,Wordpress,Class,Woocommerce,我在Woocommerce中编写了一个复杂的产品类型,我不喜欢大多数WordPress代码的外观(在我看来,默认情况下有点凌乱,而且“没有诗意”),所以我决定使用PHP名称空间和自动加载。所以我必须在WordPress可以获取的范围内声明我的产品类型类。以下是实际的做法 add_action('init', function(){ class WC_Product_Partner_Aware extends \WC_Product_Variable{ // Content

我在Woocommerce中编写了一个复杂的产品类型,我不喜欢大多数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
    }
});
我想在一个名称空间中声明这个类,比如has
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(\some\namespace\WC_Product_Partner_Aware)){
        $ppa = new \some\namespace\WC_Product_Partner_Aware();
        $ppa->doStuff();
    }

});
WC_Product_Partner_Awre.php

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

如何管理自动加载?为什么不使用require?@Yanis git它使代码看起来更整洁,而且不必担心
require
使大型项目的工作更轻松