我有我的答案,但似乎有很多人投了赞成票https://wordpress.stackexchange.com/a/48094/145078
但我不知道为什么它对我不起作用。。
作为回答
class MyClass {
function __construct() {
add_action( \'init\',array( $this, \'getStuffDone\' ) );
}
function getStuffDone() {
// .. This is where stuff gets done ..
}
}
$var = new MyClass();
我不需要设置可见性吗?namespace NS{
class MyClass {
public function __construct() {
add_action( \'init\',array( $this, \'getStuffDone\' ) );
}
public function getStuffDone() {
// .. This is where stuff gets done ..
}
}
}
$var = new MyClass();
以上代码不适用于我,是否正确或我方有任何错误?但是这个代码很好用
add_action(\'init\',function(){
$lat = new \\NS\\MyClass();
$lat->getStuffDone(); //Sorry not $lath
});
我正在按要求调用类文件get\\u template\\u directory()/myfolder/class/MyClass。php