这可能是一个重复的问题,但是...我读到这里的几个答案,并在约类的属性(变量)和如何申报他们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;
_class_是大写的。 $ POST不是一个有效的PHP数组($ _POST是),如果没有设置$ POST ['id'],该怎么办? – pavlovich
恭喜,你指出了一些错别字,让我编辑现在 – Kisaragi