我试图用函数将一组php5变量调用到另一个页面中,但当我运行代码时,它会生成页面错误。以下是我的代码:
<?php function SocialLinks(){
$blogger_icon = $this->params->get('blogger_icon');
$digg_icon = $this->params->get('digg_icon');
$facebook_icon = $this->params->get('facebook_icon');
$stumble_icon = $this->params->get('stumble_icon');}?>
<?php SocialLinks();//code to call my function in another page ?>
有人能告诉我该怎么做吗?
此时,您调用函数SocialLinks(),变量在函数内部分配。但在功能之外无法访问它们。
如果你想在函数之外使用变量,你需要返回它们的内容。例如:
class SocialLinks{
private $bloggerIcon;
private $diggIcon;
private $facebookIcon;
private $stumbleIcon;
public function __construct(){
$this->bloggerIcon = $this->params->get('blogger_icon');
$this->diggIcon = $this->params->get('digg_icon');
$this->facebookIcon = $this->params->get('facebook_icon');
$this->stumbleIcon = $this->params->get('stumble_icon');
}
public function getBloggerIcon(){
return $this->bloggerIcon;
}
public function getDiggIcon(){
return $this->diggIcon;
}
public function getFacebookIcon(){
return $this->facebookIcon;
}
public function getStumbleIcon(){
return $this->stumbleIcon;
}
}
然后在另一页:
$socialLinks = new SocialLinks();
$socialLinks->getBloggerIcon(); //return the blogger icon
使用JFactory在需要时获取模板参数:
$params = JFactory::getApplication()->getTemplate(true)->params;
您可以访问所需的数据。
$bloggerIcon = $params->get('blogger_icon');