2015-11-13 30 views
0

我想调用一组php5变量到一个函数的另一个页面,但是当我运行代码时,它正在生成一个页面错误。以下是我的代码:如何使用函数调用一组PHP变量?

<?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 ?> 

请有人可以告诉我ho去做这件事吗?

+0

你能分享一下返回的错误吗? – Nirnae

+0

在调用它之前是否包含具有该功能的文件? – Andrius

+0

在我的谷歌浏览器中显示“500 Internal server”错误@Nirnae 我在调用它之前包含了该文件。 –

回答

0

此刻,您调用SocialLinks()函数并将变量分配给里面的函数。但他们不能在功能之外访问。

如果您想使用函数以外的变量,则需要返回其内容。对于exmple:

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 
+0

我在PHP中不太好,但我认为这与我所问的是相反的。 我正在建造joomla!模板,我只想使用上述变量从templateDetails.xml文件中提取链接。 这些变量完美地工作,但我只想从另一个页面调用它们,因为它们非常多,使index.php干净,以加载快。 –

0

使用JFactory去模板参数当你需要他们:

$params = JFactory::getApplication()->getTemplate(true)->params; 

的,你可以访问你的数据需要。

$bloggerIcon = $params->get('blogger_icon');