强制执行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);
没有评论▼