Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有PDO连接的OOP PHP,包括不同的页面类_Php_Class_Oop_Pdo_Extends - Fatal编程技术网

带有PDO连接的OOP PHP,包括不同的页面类

带有PDO连接的OOP PHP,包括不同的页面类,php,class,oop,pdo,extends,Php,Class,Oop,Pdo,Extends,类/dbConnect.php页面 class dbConnect { /* * initiate mysql database host,username,password,database name */ private $host; private $dbName; private $uname; private $upass; private $con; public function __construct

类/dbConnect.php页面

class dbConnect {
    /*
     * initiate mysql database host,username,password,database name
     */

    private $host;
    private $dbName;
    private $uname;
    private $upass;
    private $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 . ";dbName=" . $this->dbName;
        try {
            $this->con = new PDO($dsn, $this->uname, $this->upass);            
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }

}

class/sampleView.php

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



class sampleViews extends dbConnect{

     function viewUsers(){
         /*
          * dbConncect Class Connection variable access
          */
         $connection = $this->con;

     }
}
这样对吗???我无法通过sampleView类使用$this->method访问dbConnect类$con varibale


并且能够以这种方式完成不同位置类的扩展,包括页面。Help Plezz

即使在扩展父类时,您也不能访问
私有
变量。您需要将其更改为受保护的:

protected $con;
您还需要构造父类,否则它永远不会实例化父(扩展)类

上述内容将位于
类SimpleViews Extendes dbConnect{..

dbConnect页面内

改变

protected $host;
protected $dbName;
protected $uname;
protected $upass;
protected $con;
和示例视图页面

public function __construct() {
    parent::__construct('localhostr', 'test', '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";
    }
}

有人能纠正我的代码吗?

将属性从
私有
更改为
受保护的
你,比我快了2秒。@RichardChristensen Haha花了我一段时间格式化代码:这里的建议是在视图类中插入
DbConnect
对象,以消除对
父对象的需要::\u construct();
@RichardChristensen我也会这么做的!!我要说的是,我只是在回答OP的问题范围!没问题,OOP设计是我的崇拜。如果Darren的答案修复了你的代码,你应该接受他的答案,而不是发布你自己的代码。访问看看,然后回到Darren的答案,并以同样的方式勾选复选标记如图所示。除@Fred所说之外,您不希望您的
主机/username/password/db
在课堂之外可以访问。因此他们应该保持
隐私。
public function __construct() {
    parent::__construct('localhostr', 'test', '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";
    }
}