这个PDO OOP方法是正确的吗?

时间:2015-01-15 04:48:27

标签: php oop netbeans pdo

dbConnect.php dbConnect类页面

class dbConnect {

    /*
     * initiate mysql database host,username,password,database name
     */

    private $host;
    private $dbName;
    private $uname;
    private $upass;
    protected $con;

    public function __construct($host, $database, $userName, $password) {
        $this->host = $host;
        $this->dbName = $database;
        $this->uname = $userName;
        $this->upass = $password;
        $this->connectDB();
    }

    public function connectDB() {
        /*
         * @var $dsn mean data source name for pdo connection
         */
        $dsn = "mysql:host=" . $this->host . ";port=3306;charset=utf8;dbname=" . $this->dbName;
        try {
            $this->con = new PDO($dsn, $this->uname, $this->upass);
            $this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->con->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            return $this->con;            
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
}

sampleView.php sampleViews类页面

/*
 * include dbConnect class for this SampleView Class Page
 */
require_once 'class/dbConnect.php';

class sampleViews extends dbConnect {

    public function __construct() {
        parent::__construct('localhost', 'test2', 'root', '');
    }

    function chkConnection() {
        /*
         * dbConncect Class Connection variable access
         */
        $dbcon = $this->con;
        if ($dbcon) {
            echo "successfully Connect database";
        } else {
            echo "sorry Could not be connect databsae";
        }
    }

    /*
     * In CRUD method implementation
     * READ Method Sample
     */

    function getData() {
        $sql = "SELECT "
                . "users.uid, "
                . "users.uname, "
                . "users.upass "
                . "FROM "
                . "users";
        try {
            $stmt = $this->con->prepare($sql);
            $stmt->execute();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $array[] = $row;
            }
            return $array;
        } catch (Exception $exc) {
            echo $exc->getMessage();
        }
    }

    /*
     * CREATE Method
     */

    function createSample() {
        /*
         * ini variable for insert
         */
        $uname = "රුවන්";
        $upass = "123";

        //Sql insert query
        $sql = "INSERT INTO `test2`.`users` (`uname`, `upass`) VALUES (:uname, :upass);";

        try {

            /*
             * prepare Insert Query
             */
            $createStmt = $this->con->prepare($sql);
            /*
             * bindParam method use for inset user data preventing sql injection
             */
            $createStmt->bindParam(":uname", $uname, PDO::PARAM_STR, 150);
            $createStmt->bindParam(":upass", $upass, PDO::PARAM_STR, 150);
            /*
             * execute insert query
             */
            if ($createStmt->execute()) {
                echo "successfully Inserted";
            } else {
                echo "sorry could not be inserted";
            }
        } catch (Exception $exc) {
            echo $exc->getMessages();
        }
    }

}

我的问题出现在第二课,即示例视图。父类$con变量可以访问,但Net Beans IDE不显示任何建议。准备,执行,绑定Param或与PDO db Connection相关的任何事物。所以我认为我的OOP方法是错误的或不正确的。任何人都可以确认我是否正确吗?

1 个答案:

答案 0 :(得分:0)

PHP不是强类型 - 方法签名不提供有关返回类型的信息。变量没有关于类型的信息。要帮助您的IDE,您必须使用描述类型的PHPDoc注释。

在$ con property中添加评论:

class dbConnect {

    private $host;
    private $dbName;
    private $uname;
    private $upass;
    /**
    * @var PDO
    */
    protected $con;