繁体   English   中英

PHP静态函数self ::在joomla中的JFactory类解释?

[英]PHP static function self:: in joomla JFactory class explanation?

嗨,我正在查看Joomla的代码,并试图弄清楚这个函数究竟发生了什么。

index.php调用函数

$app = JFactory::getApplication('site');

jfactory.php代码

public static function getApplication($id = null, $config = array(), $prefix='J')
{
    if (!self::$application) {

        jimport('joomla.application.application');

        self::$application = JApplication::getInstance($id, $config, $prefix);
    }

    return self::$application;
}

application.php代码..

public static function getInstance($client, $config = array(), $prefix = 'J')
{
    static $instances;

    if (!isset($instances)) {
        $instances = array();
    }

    ....... more code ........

    return $instances[$client];
}

现在我无法弄清楚函数getApplication为什么是self:$ application used。

self::$application = JApplication::getInstance($id, $config, $prefix);

$ application始终为null,使用此方法的目的是什么。 我尝试修改它

$var = JApplication::getInstance($id, $config, $prefix);

并返回它但它不起作用。

如果有更多知识的人能够尽可能详细地解释这里发生的事情,我将非常高兴。 非常感谢。

self::用于访问类的静态成员。

所以在这种情况下, self::$application用于在JFactory中缓存应用程序对象,以避免多次调用更昂贵的JApplication::getInstance

有关静态的更多信息,请参阅静态关键字

getApplication() - 返回对Global JApplication对象的引用。 阅读更多

self::$member表示要访问的静态成员。

这是我能理解的解释。

if (!self::$application){ //<-check for the $application static variable of the the class

jimport('joomla.application.application');        
self::$application = JApplication::getInstance($id, $config, $prefix);

//if it does not exist get a new instance otherwise nothing happens because there is no else part 
}

return self::$application; //<- return the object(new one or the existing one)

这样做的是,如果存在$ application,则保存函数调用。 如果没有获得新实例。 阅读更多 希望这可以帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM