【发布时间】:2012-04-08 18:15:24
【问题描述】:
我尝试将 Captcha (https://github.com/kolanos/kohana-captcha) 安装到 Kohana 3.1。模块已安装,但仍然搞砸了,有问题无法找到答案。如果你特别是我不明白它是如何工作的,这里是代码:
/**
* Returns the img html element or outputs the image to the browser.
*
* @param boolean $html Output as HTML
* @return mixed HTML, string or void
*/
public function image_render($html)
{
// Output html element
if ($html === TRUE)
return '<img src="'.url::site('captcha/'.Captcha::$config['group']).'" width="'.Captcha::$config['width'].'" height="'.Captcha::$config['height'].'" alt="Captcha" class="captcha" />';
// Send the correct HTTP header
Request::instance()->headers['Content-Type'] = 'image/'.$this->image_type;
Request::instance()->headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0';
Request::instance()->headers['Pragma'] = 'no-cache';
Request::instance()->headers['Connection'] = 'close';
// Pick the correct output function
$function = 'image'.$this->image_type;
$function($this->image);
// Free up resources
imagedestroy($this->image);
}
问题:
在变量$html中,调用该方法时(默认为config)为true。因此是返回和底层代码不应该被执行,但调试器却说反了……它是如何工作的?
稍后在变量 $function 中通过连接 "image" 和 $thos->image_type 传递一个字符串(如上所示 = "png")。结果是带有函数名称的行,它给出了 png 格式的图像(“imagepng”)。并且以下行使用了晦涩的语法: $function($this->image);这些行是做什么的?
我希望有人能帮助我了解它是如何工作的。
【问题讨论】: