我为Joomla 2.5创建了一个新组件。我有两个功能:
public function getBase(){
if(JFactory::getUser()->guest) {
$this->base = 'Гость';
}
else {
$user =& JFactory::getUser();
$usr_id = $user->get('id');
/**/
$this->base = 'Гуд юзер id '.$usr_id.'';
/*Get database info*/
}
return $this->base;
}
public function getGetInfo() {
$this->getinfo = '11 '.$usr_id.'';
return $this->getinfo;
}
请告诉我如何在getGetInfo()
函数中使用getBase()
中的$usr_id = $user->get('id');
。谢谢你的帮助。
如果这两个函数在同一个类中,则可以使用类变量。
class MyClass
{
private $user;
public function getBase()
{
// ---
$user =& JFactory::getUser();
// Set user class variable
$this->user = $user;
// ---
}
public function getGetInfo()
{
// Now you can use the user
$user = $this->user;
// ---
}
}
如上所述,可以(尽管您不想这样做,因为这是代码重复),只需在getGetInfo()
方法中调用相同的代码即可获得用户。不过不要重复代码,要使用类变量。
您有两种选择来实现这一要求。
一种是从用户对象访问它,就像上面一样。
$user =& JFactory::getUser();
$user_id = $user->id;
或者你必须为类创建一个类变量,比如
public $current_user;
and inside the public function getBase(){
$this->current_user = $user->get('id');
}
则该$this->current_user
变量将在整个类函数