【问题标题】:Sharing variables between functions in a PHP class在 PHP 类中的函数之间共享变量
【发布时间】:2016-03-25 08:39:20
【问题描述】:

这可能是一个重复的问题,但是......我在这里阅读了几个答案以及 php.net 上关于类属性(变量)以及如何声明它们的信息,但我未能成功应用这些知识。更准确地说,我不能将一个变量从一个函数传递给这个类中的另一个。我的课程是为 Wordpress 构建的,示意图如下所示。所有函数的运行顺序与它们在类中的顺序相同。在getForm() 函数中,接收到具有帖子ID 的变量$_POST['postid'] 并获取具有此ID 的帖子。我需要将帖子 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;

【问题讨论】:

    标签: php class properties


    【解决方案1】:

    你可以添加任何你需要的类属性:

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

    【讨论】:

    • Class 是大写的。 $POST 不是一个有效的 PHP 数组($_POST 是),如果 $POST['id'] 没有设置怎么办?
    • 恭喜,你指出了一些错别字,让我现在编辑一下
    【解决方案2】:

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

    private $post_id;
    

    然后在你的constructor 中你可以这样做:

    $this->post_id = $_POST['postid'];
    

    现在在您的任何类方法中,$post_id 都可以作为$this-&gt;post_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);
        }
    
    }
    

    【讨论】:

    • 我用echo 'Post ID is ' . $this-&gt;post_to_edit_id;检查了每个类函数,如果$this-&gt;post_id具有帖子ID作为值并且除了handleForm()函数之外一切正常,这里$this-&gt;post_id什么都不返回(或不存在,我不知道)。如果需要,您可以通过下一个链接查看原始代码。在那里,我用两个隐藏的输入字段解决了我的问题,但我不喜欢这样。 wordpress.stackexchange.com/a/212165/25187
    • 我检查了这个类中的每一行代码,但我不明白为什么 $this-&gt;post_id 属性只在 handleForm() 函数中是干净的(没有任何值)。可能是因为这个函数是这样执行的:add_action( 'template_redirect', array( $this, 'handleForm' ) );?
    猜你喜欢
    • 2017-11-19
    • 2017-03-27
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多