【发布时间】: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