我试图将验证码(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);
}
。这不属于这个班级 http://prog-school.ru/forum/go.php?https://github.com/kolanos/kohana-captcha/blob/master/classes/captcha.php
问题:
在变量$html中,调用此方法(默认为 config)时为 true。因此是返回,不应执行底层代码,但调试器说相反...它是如何工作的?
稍后在变量$function通过连接"image"和$thos->image_type传递一个字符串(如上所示="png")。结果是带有函数名称的行,该函数以 png 格式("imagepng")提供图像。以下行使用了晦涩的语法:$function($this->图像);这些线条有什么作用?
我希望有人能帮助我了解它是如何工作的。
- 配置中的任何位置都没有设置
html
值。仅当$html为 TRUE 时才返回,即image_render(TRUE)
.如果由于语句if
中使用的运算符===
而调用image_render(1)
或image_render('some string')
,则不会执行它。查看此处以了解有关转换为布尔类型的更多信息。 - 第一行计算函数名称(例如 imagepng),第二行调用此函数。有关更多详细信息,请参阅变量函数。