Home  >  Article  >  Backend Development  >  thinkphp在模型中自动完成session赋值示例代码_php技巧

thinkphp在模型中自动完成session赋值示例代码_php技巧

WBOY
WBOYOriginal
2016-05-16 20:36:251036browse

相信用过thinkphp的用户都知道thinkphp的模型可以完成很多辅助功能,比如自动验证、自动完成等,今天在开发中遇到自动完成中需要获取session值然后自动赋值的功能,具体看代码;

class ArticlelModel extends Model {

protected $_auto = array (
array('addtime','time',1,'function'),
array('username','getName',1,'callback')
);

//这个函数获取session里的name值
protected function getName(){
return $_SESSION["name"];
}
}

这里需要注意最后一个参数function和callback的区别;
function:使用函数,会自动去Common/common.php去寻找对应的函数;
callback:使用在当前模型中定义的回调方法

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn