728x90

<?php
class DBController {
    private $host = 'localhost';
    private $database = 'test';
    private $userid = 'root';
    private $password = 'autoset';
    protected $db; // 변수를 선언한 클래스와 상속받은 클래스에서 참조할 수 있다.

    public function __construct() {
        $this->db = $this->connectDB();
        // construct 메소드는 객체가 생성(인스턴스화)될 때 자동으로 실행되는 특수한 메소드다.
    }

    function __destruct(){
        mysqli_close($this->connectDB());
        //mysqli_close($this->db);
    }

    private function connectDB() {
        $dbconn = mysqli_connect($this->host, $this->userid, $this->password, $this->database);
        mysqli_set_charset($dbconn, "utf8"); // DB설정이 잘못되어 euc-kr 로 되어 있으면 문제가 됨
        if (mysqli_connect_errno()) {
           printf("Connect failed: %s\n", mysqli_connect_error());
           exit();
        } else {
          return $dbconn;
        }
    }

}//end dbClass
?>

<?php
class LoginClass extends DBController {
// class 자식클래스 extends 부모클래스
    // 회원 가입 여부 체크
    public function isUserExisted($u) {
        if(!isset($u) || empty($u)) {
            return '-1';
        } else {
            $sql ="SELECT count(userID) from members WHERE userID='".$u."'";
            $result = mysqli_query($this->db, $sql);
            if($row = mysqli_fetch_array($result)){
                return $row[0]; // 미가입이면 0 반환, 가입이면 1 반환
            } else {
                return -1;
            }
        }
    }
}//end class LoginClass
?>


PHP 함수를 만들어서 사용하는 경우 개수를 체크하기 위해 count(*)를 사용한다.
이 경우 반환값은 정수일까? String(문자열)일까?

아래 코드를 직접 실행해보면 결과가 나온다.
변수 타입 확인은 getttype 함수 또는 var_dump 함수로 확인할 수 있다.
<?php
$userID ='jsk005@naver.com';
include_once 'dbController.php';
require_once 'loginClass.php';
$c = new LoginClass();
$rs = $c->isUserExisted($userID);

if($rs === 1){
    echo 'Variable Type is Integer<br />';
} else if($rs == '1'){
    echo '
Variable Type is string <br />';
    echo gettype($rs);
    //var_dump($rs);
}
?>

결과는 정수 타입이 아니라 String 이다.


블로그 이미지

Link2Me

,