私は CakePHP を使用しており、次のようにクラスを作成しました。
class ApiController extends AppController {
// functions
}
クラスには約 10 個の関数があり、すべての関数の先頭にまったく同じ 3 行のコードを繰り返していることがわかりました。
if ($this->request->is('post')) {
$data = $this->request->input('json_decode',true);
$authUser = explode('.',$_SERVER['PHP_AUTH_USER']);
$location_id = $authUser[1];
// Rest of my function
}
これらの 3 行のコードを最初に実行し、次に $data 変数と $location_id 変数を関数で使用できるようにするクラスで何かを作成する方法はありますか、またはすべての関数に対してこれらの 3 行を記述する必要がありますか?