Php 如何获取函数的变量
我有一个WordPress小部件,但我不知道如何从form函数获取类下面my_自定义函数的$title变量。我尝试了这个新手解决方案,但没有成功Php 如何获取函数的变量,php,plugins,wordpress,Php,Plugins,Wordpress,我有一个WordPress小部件,但我不知道如何从form函数获取类下面my_自定义函数的$title变量。我尝试了这个新手解决方案,但没有成功 class My_Widget extends WP_Widget { public function __construct() { } public function widget( $args, $instance ) { } public function form( $instance ) { if ( isse
class My_Widget extends WP_Widget {
public function __construct() {
}
public function widget( $args, $instance ) {
}
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = 'New title';
}
}
public function update( $new_instance, $old_instance ) {
}
}
function my_custom(){
$my_title = $instance[ 'title' ];
echo $my_title;
}
首先创建类的对象,然后获取类变量
class My_Widget extends WP_Widget {
public $mytitle;
public function __construct() {
}
public function widget( $args, $instance ) {
}
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$this->mytitle = $instance[ 'title' ];
}
else {
$this->mytitle = 'New title';
}
}
public function update( $new_instance, $old_instance ) {
}
}
function my_custom(){
$widgetobj=new My_Widget();
$my_title = $widgetobj->mytitle;
echo $my_title;
}
表单函数$title中的else后面缺少结尾},应将其指定为属性
$this->title=“New title”代码>在表单函数中