在PHP类中的函数之间共享变量

在PHP类中的函数之间共享变量,php,class,properties,Php,Class,Properties,这可能是一个重复的问题,但是。。。我在这里读了几个答案,以及php.net上关于类属性(变量)和如何声明它们的信息,但是我没有成功地应用这些知识。更确切地说,我不能在这个类中将变量从一个函数传递到另一个函数。我的类是为Wordpress构建的,示意图如下所示。所有函数的运行顺序与它们在类中的运行顺序相同。在函数的getForm()中,接收一个带有POST ID的变量$\u POST['postid'],并获取带有此ID的POST。我需要的是将post ID传递给handleForm()函数,但我

这可能是一个重复的问题,但是。。。我在这里读了几个答案,以及php.net上关于类属性(变量)和如何声明它们的信息,但是我没有成功地应用这些知识。更确切地说,我不能在这个类中将变量从一个函数传递到另一个函数。我的类是为Wordpress构建的,示意图如下所示。所有函数的运行顺序与它们在类中的运行顺序相同。在函数的
getForm()
中,接收一个带有POST ID的变量
$\u POST['postid']
,并获取带有此ID的POST。我需要的是将post ID传递给
handleForm()
函数,但我失败了。每次尝试某项操作时,我都会收到一条消息,说明我的变量没有声明。在这门课上如何正确地做到这一点

class WPSE_Submit_From_Front {

    function __construct() {
        ...
        add_action( 'template_redirect',  array( $this, 'handleForm' ) );
    }

    function post_shortcode() {
        ...
    }

    function getForm() {

        if( 'POST' == $_SERVER['REQUEST_METHOD'] && isset( $_POST['postid'] ) ) {
            $post_to_edit = array();
            $post_to_edit = get_post( $_POST['postid'] );
            // I want to use the $post_to_edit->ID or
            // the $_POST['postid'] in the handleForm() function
            // these two variables have the same post ID
        }

        ob_start();
        ?>

        <form method="post">
            ...
        </form>

        <?php
        return ob_get_clean();
    }

    function handleForm() {
        ...
    }

}

new WPSE_Submit_From_Front;
WPSE类从前端提交{
函数_u构造(){
...
添加动作('template_redirect',数组($this,'handleForm');
}
函数post_短码(){
...
}
函数getForm(){
if('POST'=$\u服务器['REQUEST\u方法']&&isset($\u POST['postid'])){
$post_to_edit=array();
$post_to_edit=get_post($_post['postid']);
//我想使用$post\u编辑->ID或
//handleForm()函数中的$\u POST['postid']
//这两个变量具有相同的post ID
}
ob_start();
?>
...

您可以添加所需的任何内容作为类属性:

Class WPSE_Submit_From_Front {
     public $post_id;
     public function set_post_id()
     { 
          $this->post_id = $POST['id'];
     }
 }

好的,那么在类中可以声明私有变量:

private $post_id;
然后在
构造函数中
可以执行以下操作:

$this->post_id = $_POST['postid'];
现在,在您的任何类方法中,$post\u id都可以作为
$this->post\u id

在您的情况下,它将如下所示:

class WPSE_Submit_From_Front {

    private $post_id;

    function __construct() {
        $this->post_id = $_POST['postid'];
    }

    function post_shortcode() {
        $somevar = $this->post_id;        
    }

    function getForm() {

        if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $this->post_id ) ) {
            $post_to_edit = array();
            $post_to_edit = get_post( $this->post_id );
            // ...
        }

        // ...
    }

    function handleForm() {
        do_something_new($this->post_id);
    }

}

类是大写的。$POST不是有效的PHP数组($\u POST是),如果$POST['id']未设置?恭喜,你指出了一些错误,现在让我编辑一下,我用
echo'Post ID is.$this->Post_to_edit_ID;
检查了每个类函数,如果
$this->Post_ID
将Post ID作为一个值,并且除了
handleForm()
函数,这里的
$this->post\u id
不返回任何内容(或者不存在,我不知道)。如果您愿意,可以通过下一个链接查看原始代码。在那里,我解决了两个隐藏输入字段的问题,但我不喜欢这样。我检查了这个类中的每一行代码,但我不明白为什么
$this->post\u id
属性仅在
handleForm()中是干净的(没有任何值)
函数。可能是因为此函数由以下命令执行:
添加操作('template\u redirect',array('this,'handleForm'));