duaabhuv188411 2014-10-02 07:21
浏览 30
已采纳

在php中的子类中使用时,parent方法返回空

I have a parent class (DB) with a public method named con().
This method should return whatever is inside private $con - which it does both inside the class it self, and from outside.

class DB
{
    private $con = array();

    public function add($alias, $db, $un, $pw, $desc)
    {
        try {
            $options = array(/*PDO OPTIONS*/)
            $this->con[$alias] = array(
              'alias' => $alias, 
              'db' => $db, 
              'dbh' => new PDO('mysql:host=localhost;dbname='.$db.';charset=utf8', $un, $pw, $options), 'desc'=>$desc);
        } catch(PDOException $e) {
            /*  return error message  */
        }
    }

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

I'm adding connections like this:

$db = new DB();
$db->add('alias1','databasename','username','password','an optional short description');
$db->add('alias2','databasename2','username2','password2','');

Now, the private $con should be populated with two connections - which it is.
And the con() method should now return these connections - which it also does:

I put up this test function inside the KD-class:

public function aliases()
{
    foreach($this->con() as $con){    //  this should returns an array with the aliases.
        $aliases[] = $con['alias'];
    }
    return $aliases;
}

The following code returns: array(2) { [0]=> string(4) "alias1" [1]=> string(4) "alias2" }

<?=var_dump($db->aliases())?>

Now, I also created useAliases() as a test method inside a child class that extends the DB class:

class SQL extends DB
{
    public function useAliases()
    {
        var_dump($this->aliases());
    }
}

The following code returns: NULL NULL

$sql = new SQL();
var_dump($sql->useAliases());

Why does this happen? I expected that the useAliases() would return the same as aliases().

  • 写回答

1条回答 默认 最新

  • drugs3550 2014-10-02 07:22
    关注

    You need to use method add() from parent class to set up your connection. Now default value of $con attribute is array() because you defined it so:

    private $con = array();
    

    so you get what is expected.

    If you want it to work you need to runt it this way:

    $sql = new Sql();
    $sql->add('alias','db','un','pw','desc');
    $sql->showTables();
    

    EDIT

    You cannot do it this way:

    $db = new DB();
    $db->add('alias1','databasename','username','password','an optional short description');
    $db->add('alias2','databasename2','username2','password2','');
    $sql = new Sql();
    $sql->showTables();
    

    because $db and $sql in this case are separate objects. You need to do it as I showed above

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!