可以通过URL或POST接受参数的函数


Functions that can take parameters via the URL or POST

我遇到过这样一种情况,有时我需要通过post传递参数给函数,例如实时数据的ajax调用,以及传递参数的默认样式,即myFunc($param)

我已经开始这样做了:

public function createNote($note = null)
  {
    if($note == null) // If param didn't come in by a call or url...
    {
      $note = $_POST['note']; // Use the post data.
    }
    // Do stuff with $note...
  }

是我做错了什么,还是这是最好的方法?

由于使用的是默认参数,下面是一个小改进。

public function createNote($note = null)
  {
    if(!empty($note) && validate($note)) //if inside class use $this->validate($note)
    {
         // Do stuff with $note...
    }
  }
public function validate($note){
    //do your validations here
}

使用createNote($_POST['note']);

直接将URL参数传递给函数也不是一个好的做法。在使用之前,您需要首先验证它们。