1

我正在与代码作斗争:

$worker = new Worker(); // :|
$worker->addWorker;
//testing                   
//testing
// $_SESSION['worker']->addWorker();
echo $_POST['name'];
echo $worker->name;

最后两行应该显示相同的值。类中的 assign 方法如下所示:

public function addWorker()
{
      $this->name = $_POST['name'];
      $this->surname = $_POST['surname'];
      $this->dob = $_POST['dob'];
      $this->skills = $_POST['skills'];
      $this->postcode = $_POST['postcode'];
      $this->street = $_POST['street'];
      $this->email = $_POST['email'];
      $this->tel = $_POST['tel'];
      $this->erefnumber = $_POST['erefnumber'];
}

我已经声明了类 Worker 属性。问题是我没有任何错误, $worker->name 只是给出了任何东西。echo $_POST['name'] 工作正常。

4

1 回答 1

4

您的代码几乎没问题,但是您可以:

$worker->addWorker;

虽然它应该是:

$worker->addWorker();

此外,为了提高代码的可读性,我会考虑更改addWorker()为 require 数组参数:

public function addWorker( array $postArray ) {
    $this->name = $postArray['name'];
    ....

$_POST在调用时通过:

$worker->addWorker( $_POST );
于 2012-12-02T15:32:04.030 回答