【问题标题】:Variable in Joomla ModuleJoomla 模块中的变量
【发布时间】:2011-06-16 09:52:55
【问题描述】:

我把头撞到了墙上……我正在制作一个简单的 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();

【问题讨论】:

  • 你是如何调用类 modReservationHelper 的函数的?

标签: php class module joomla


【解决方案1】:

您正在以静态方式调用该类。所以类中的 $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();

【讨论】:

  • 我得到的第一个选项:致命错误:从上下文“JModuleHelper”调用受保护的 modReservationHelper::__construct()
  • 哎呀..我没注意到。将此函数 __construct 公开。
猜你喜欢
  • 2014-09-18
  • 1970-01-01
  • 2016-10-11
  • 2010-11-20
  • 2013-04-16
  • 2015-03-10
  • 2013-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多