引用时,私有变量返回空白,尽管它是在函数中设置的

I have a class to create forms and a function that gets the form name form action and form type from the view files. It works fine and as expected, but when I call those variables in another function to create the actual form and to add the values to the they return as blank.

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

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

This is where the functions values get stored in the private variables.

When I try to call them in another function they return as blank.

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

Am I missing something ?

Your class must be namespaced to use that constructor function. Instead, use the method __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;
    }
}

Documentation

You're writing PHP 4.x OOP.

Try this:

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();