[PHP] GET 또는 POST로 날아온 파라메터를 클래스에 멤버 변수에 쉽게 넣는 방법

<?
 //----------------------------------------------------------------
 //GET 또는 POST로 날아온 파라메터를 클래스에 쉽게 넣는 방법
 //----------------------------------------------------------------

 class CUser
 {
  protected $seqNum;  //가입번호
  protected $userId;    // 유저 아이디
  protected $passwd;   // 비밀번호
  protected $userName;  //유저 이름
  protected $userNick;    //유저 별명
  protected $email;        // 이메일
  protected $jnumber;    // 주민등록번호
  protected $phone;       // 유선 전화번호
  protected $mobile;      // 핸드폰 번호
  protected $zipcode;     // 우편번호
  protected $addr1;        // 주소1
  protected $addr2;        // 주소2
  protected $regDate;     // 가입시간
  protected $userLevel;   // 레벨
  protected $userScore;   // 점수

  // 매직 메소드(PHP5부터 지원함: (사용예)$user->userName. )
  public function __set($name, $value)
  {
   $this->{$name}=$value;
  }

  public function __get($name)
  {
   return $this->{$name};
  }
 }

 $user= new CUser();

 // GET 또는 POST로 날아온 파라메터를 클래스에 쉽게 넣는 방법
 foreach($_GET as $name=> $value)
 {
  // 클래스의 $name의 값과 이름이 같은 멤버변수에 $value 값이 세팅된다.
  // 파라메터 이름과 멤버변수 이름은 대소문자를 구분함

  $user->$name = $value;
 }

 echo $user->userName."<br>";
 echo $user->userId."<br>";
?>

댓글