0

我有一个创建表单的类和一个从视图文件中获取表单名称表单操作和表单类型的函数。它工作正常并且符合预期,但是当我在另一个函数中调用这些变量以创建实际表单并将值添加到它们时,它们返回为空白。

class Form {
     private $formname;
     private $formaction;
     private $formtype;

function Form($form_name, $actionfile, $formtype) {
            $this->formname = $form_name;
            $this->formaction = $actionfile;
            $this->formtype = $formtype;
        }

这是函数值存储在私有变量中的地方。

当我尝试在另一个函数中调用它们时,它们返回为空白。

function formstart() {
$enc = 'enctype="multipart/form-data"';
$returnstring = '<form name="' . $this->formname . '" id="' . $this->formname . '" ' . $enc . ' method="' . $this->formtype . '"  action="' . $this->formaction . '">';
}

我错过了什么吗?

4

2 回答 2

3

您的类必须命名空间才能使用该构造函数。相反,使用该方法__construct()

class Form {
     private $formname;
     private $formaction;
     private $formtype;

    function __construct($form_name, $actionfile, $formtype) {
        $this->formname = $form_name;
        $this->formaction = $actionfile;
        $this->formtype = $formtype;
    }
}

文档

于 2013-01-12T23:47:33.347 回答
1

您正在编写 PHP 4.x OOP。

尝试这个:

class Form {

  private $formname;
  private $formaction;
  private $formtype;

  public function __construct($form_name, $action_file, $form_type) {
      $this->formname = $form_name;
      $this->formaction = $action_file;
      $this->formtype = $form_type;
  }

  public function formstart() {
      $enc = 'enctype="multipart/form-data"';
      return '<form name="' . $this->formname . '" id="' . $this->formname . '" ' . $enc . ' method="' . $this->formtype . '"  action="' . $this->formaction . '">';
  }
}

$f = new Form('name', 'action', 'type');
echo $f->formstart();
于 2013-01-12T23:48:55.907 回答