2011-01-27 82 views
0

我正在將我的頭撞到牆上....我正在做一個簡單的joomla模塊,在helper.php中我不能指定從表單發佈的值。Joomla模塊中的變量

<?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

你好嗎調用類modreservationHelper的函數? – Gaurav 2011-01-27 09:13:02

+0

請參閱上面的編輯。 – Jim 2011-01-27 09:25:29

回答

2

要調用靜態方法的類。所以這$類不會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();