所以,我已经关注这个问题好几天了。我正试图进一步提高我在PHP开发方面的技能,并决定采用面向对象的方法来处理一个WP插件。我知道WP不一定是最好的选择,但我喜欢好的挑战。
我正在编写一个助手类,因为我打算定期使用一些函数,但我遇到了一点问题。
助手类
Class Helper implements HelperInterface
{
/**
* Default Options for making an admin page
*
* @var array
*/
public $default_options = [
\'slug\' => \'\',
\'title\' => \'\',
\'page_title\' => \'\',
\'parent\' => null,
\'id\' => \'\',
\'capability\' => \'update_core\',
\'icon\' => \'dashicons-admin-generic\',
\'position\' => null,
\'file_name\' => null,
\'desc\' => \'\',
];
/**
* Store options passed in by user.
*
* @var array
*/
public $options = [];
public $parent_id;
public $settings_id;
/**
* Stores the media type for a stylesheet
*
* @var string
*/
public $media_type;
public function __construct( )
{
}
public function add_new_page( $opt )
{
$this->options = array_merge( $this->default_options, $opt );
add_action( \'admin_menu\', function() {
add_menu_page(
$this->options[\'page_title\'],
$this->options[\'title\'],
$this->options[\'capability\'],
$this->options[\'slug\'],
array($this, display_page_template),
$this->options[\'icon\'],
$this->options[\'position\']
);
} );
}
助手类用法
Class GoogleMaps
{
protected $helper;
public function __construct()
{
$this->helper = new Helper;
$this->helper->add_new_page([
\'slug\' => \'google-maps\',
\'title\' => \'EdsGoogleMaps\',
\'page_title\' => \'EdsGoogleMaps\',
\'capability\' => \'update_core\',
\'icon\' => \'dashicons-admin-generic\',
\'position\' => null,
\'file_name\' => \'GoogleMapsHome.php\',
]);
$this->helper->add_new_page([
\'slug\' => \'google-maps-2\',
\'title\' => \'EdsGoogleMaps2\',
\'page_title\' => \'EdsGoogleMaps2\',
\'capability\' => \'update_core\',
\'icon\' => \'dashicons-admin-generic\',
\'position\' => null,
\'file_name\' => \'GoogleMapsHome.php\',
]);
$this->helper->add_new_page([
\'slug\' => \'google-maps-3\',
\'title\' => \'EdsGoogleMaps3\',
\'page_title\' => \'EdsGoogleMaps3\',
\'capability\' => \'update_core\',
\'icon\' => \'dashicons-admin-generic\',
\'position\' => null,
\'file_name\' => \'GoogleMapsHome.php\',
]);
}
}
电流输出
<我尝试过将数组放入foreach循环。不起作用,因为,将两次返回第一个或再次返回所有三个最初的路线是一种参考传递型的东西。从函数传递变量并将其存储在类中的全局变量中。由于所描述的相同问题,仍然不起作用,我目前的想法是,这与add\\u操作和匿名函数的使用有关,同时还与add\\u操作在每次调用helper函数时都会被调用这一事实有关。
希望有人对此有某种解决方案的想法,希望我对我的问题已经足够清楚了。
谢谢:)