我正在把我的头撞到墙上......我正在制作一个简单的joomla模块,在helper.php我无法分配从表单发布的值。
<?php
// no direct access
defined('_JEXEC') or die('Restricted access');
class modReservationHelper {
public $name;
public $email;
public $message;
public $comment;
protected function __construct() {
$this->name = $_POST['fullname'];
$this->email = $_POST['email'];
$this->message = $_POST['message'];
$this->comment = $_POST['comment'];
}
function validateForm() {
echo $this->name; //The output is always 0
echo $this->email+"</br>";//The output is always 0
echo $this->message;//The output is always 0
//When I try
echo $_POST['comment']; // Is correct
}
}
?>
此外,我已经尝试过不使用具有相同零效果的构造函数:(
<?php
// no direct access
defined('_JEXEC') or die('Restricted access');
class modReservationHelper {
public $name;
public $email;
public $message;
public $comment;
function getValues() {
$this->name = $_POST['fullname'];
$this->email = $_POST['email'];
$this->message = $_POST['message'];
$this->comment = $_POST['comment'];
}
function validateForm() {
modReservationHelper::getValues;
echo $this->name; //The output is always 0
echo $this->email+"</br>";//The output is always 0
echo $this->message;//The output is always 0
//When I try
echo $_POST['comment']; // Is correct
}
}
?>
整个过程从“mod_wreservation.php”调用我调用modReservationHelper :: validateForm();
答案 0 :(得分:2)
您正在以静态方式调用该类。所以类中的$ this不是modReservationHelper的对象。
在mod_wreservation.php中使用它的正确方法是
$helperObj = new modReservationHelper(); // your choice will work (__counstruct) with this
$helperObj->validateForm();
第二选择
$helperObj = new modReservationHelper();
$helperObj->setValue();
$helperObj->validateForm();
和班级将
<?php
// no direct access
defined('_JEXEC') or die('Restricted access');
class modReservationHelper {
public $name;
public $email;
public $message;
public $comment;
function setValues() {
$this->name = $_POST['fullname'];
$this->email = $_POST['email'];
$this->message = $_POST['message'];
$this->comment = $_POST['comment'];
}
function validateForm() {
echo $this->name; //The output is always 0
echo $this->email+"</br>";//The output is always 0
echo $this->message;//The output is always 0
//When I try
echo $_POST['comment']; // Is correct
}
}
?>
如果你在mod_wreservation.php
中使用它会更好$post = JRequest::get('post');
$helperObj = new modReservationHelper();
$helperObj->setValue($post);
$helperObj->validateForm();