哪一天 哪一天 我有吃有穿有住有钱 不再流浪 流浪
« »

强制执行parent::__construct(), self才是当前的对象

<?php
class A
{
    function __construct()
    {
      echo ‘construct’;
    }

    function test()
    {
       echo ‘test’;
    }
}

class B extends A
{
function __construct()
{
       // 正确的写法
       parent::__construct();
       self::test();

       //错误的写法
       //$b = parent::__construct();
       //$b->test();
   }
}

$b = new B();
?>

今天在这上面花了不少时间,还是对oop的理解不是很深刻才会造成这个错误.
这是我用zendframework写的认证

<?php
/*
* Created on 2008-5-17 01:23:00
*
* Auth.php
*
* @author: sanp
* @blog: http://hi.baidu.com/tecent
*/
class Sanp_Auth extends Zend_Auth_Adapter_DbTable
{
    public function __construct($userName, $userPassword)
    {  
        $dbAdapter = Zend_Db_Table::getDefaultAdapter();
        parent::__construct($dbAdapter, ‘users’, ‘user_name’, ‘user_password’);
       
        // set credentials value
        self::setIdentity($userName);
        self::setCredential($userPassword);
       
        // run credential query and save the result
        $result = self::authenticate();
    }
}

每次login的时候调用下这个就可以了.
$auth = new Sanp_Auth($userName, $userPassword);

随机日志

日志信息 »

该日志于2008-05-18 12:33由 admin 发表在程序设计分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下引用到你的网站或博客,还可以通过RSS 2.0订阅这个日志的所有评论。

主机推荐 »

赞助商链接 »

没有评论

发表评论 »

返回顶部