3

我有一个问题,了解类扩展的工作原理..

我正在尝试扩展一个类以拆分不同文件中的函数以使其更有条理..

但是我在将主类的变量和函数访问到子类时遇到问题。

这就是我所拥有的:

父类:是uFlex Class v 0.88 我就不全写了,因为太长了..

class uFlex {
    //Constants
    const version = 0.88;
    const salt = "";
    //End of constants\\\\
    /**
     * PDO / database credentials
     */
    var $db = array(
        "host" => '',
        "user" => '',
        "pass" => '',
        "name" => '',   //Database name
        "dsn" => '' //Alterntive PDO DSN string
    );

        function connect(){
        if(is_object($this->db)) return true;

        /* Connect to an ODBC database using driver invocation */
        $user = $this->db['user'];
        $pass = $this->db['pass'];
        $host = $this->db['host'];
        $name = $this->db['name'];
        $dsn = $this->db['dsn'];

        if(!$dsn){
            $dsn = "mysql:dbname={$name};host={$host}";
        }

        $this->report("Connecting to database...");

        try{
            $this->db = new PDO($dsn, $user, $pass);
            $this->report("Connected to database.");
        }catch(PDOException $e){
            $this->error("Failed to connect to database, [SQLSTATE] " . $e->getCode());
        }

        if(is_object($this->db)) return true;
        return false;
    }
}

然后:

<?php
class admin extends uFlex {

    function adm_getUsers(){
            if(!$this->connect()) return false;

            $sql= "SELECT * from users LIMIT 30";
            $st = $this->db->prepare($sql);
        $out = $st->execute();
            $row = $st->fetchAll(PDO::FETCH_ASSOC);
            return $row;    
    }

    function adm_getSingleUser($id){
            if(!$this->connect()) return false;
        if(is_numeric($id)){
            $sql= "SELECT * from users WHERE id = '$id'";
            }else{
            $sql= "SELECT * from users WHERE username = '$id'";
            }
            $st = $this->db->prepare($sql);
        $out = $st->execute();
            $row = $st->fetch(PDO::FETCH_ASSOC);
            return $row;
    }
}

?>

我在每个页面中包含的配置文件中初始化它们:

$user = new uFlex(false);
$admin = new admin();

但是当使用$admin->adm_getUsers();$row 数组时,它是空的。

在尝试在 2 个类之间拆分函数之前,我在主类上使用了相同的函数,并且正在工作。

这是我第一次尝试扩展一个类..我在谷歌上搜索,也在这里阅读了一些问题,但这对我来说太复杂了,因为我还在学习 PHP。

4

3 回答 3

7

这就是继承并不是最好的答案的地方。相反,您可以放弃继承并改用组合。将您的实例uFlex作为依赖项传递Admin,如下所示:

$user = new uFlex(false);
$admin = new Admin($user); // uFlex is being passed in

您首先需要更新您的 PHP 类,因为有一些更改:

class Admin {

    // Added private variable that will hold the uFlex instance
    private $user;

    // Added a class constructor which will be called when we create a new Admin
    function __construct($user) { // Receives an instance of uFlex
        $this->user = $user;
    }

    function adm_getUsers(){
        if(!$this->user->connect()) return false; // Call connect on user
        $sql= "SELECT * from users LIMIT 30";
        $st = $this->user->db->prepare($sql); // Call prepare on db of user
        $out = $st->execute();
        $row = $st->fetchAll(PDO::FETCH_ASSOC);
        return $row;    
    }

    function adm_getSingleUser($id){
        if(!$this->user->connect()) return false; // Same here
        if(is_numeric($id)) {
            $sql= "SELECT * from users WHERE id = '$id'";
        } else {
            $sql= "SELECT * from users WHERE username = '$id'";
        }
        $st = $this->user->db->prepare($sql); // And here
        $out = $st->execute();
        $row = $st->fetch(PDO::FETCH_ASSOC);
        return $row;
    }
}
于 2013-02-21T09:38:00.170 回答
0

如果你想调用 adm_getUsers(),你应该写 $user = new admin()。您必须在管理类中创建 _ constuct() 方法,该方法将调用 parent:: _construct($val)。

于 2013-02-21T09:37:39.230 回答
0

当你扩展一个类时,你实际上是在访问父类的属性和方法,而不是执行它们。因此,如果您想从父类中的某个方法访问某些数据,则必须在父类中执行该特定方法,然后访问它。

例如:

<?php
class parent{

    public function adm_getUsers(){
        //your code to get users
    }
}

class child extends class parent{
    public $adm_getUsers = array();

    public function getUsers(){
        if(!isset($this->adm_getUsers)){
            $this->adm_getUsers = $this->adm_getUsers();
        }
        return $this->adm_getUsers;
    }
}

$childObj = new child();
$users = $childObj->getUsers();
于 2013-02-21T09:46:32.670 回答