简体   繁体   English

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

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

Hi I'm looking at the code of Joomla and trying to figure out what exactly happends in this function. 嗨,我正在查看Joomla的代码,并试图弄清楚这个函数究竟发生了什么。

index.php makes a call to function index.php调用函数

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

jfactory.php code 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 code.. application.php代码..

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

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

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

    return $instances[$client];
}

Now I cannot figure out in function getApplication why is self:$application used. 现在我无法弄清楚函数getApplication为什么是self:$ application used。

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

$application is always null, what is the purpose of using this approach. $ application始终为null,使用此方法的目的是什么。 I tryied modifying it to 我尝试修改它

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

and returnig it but it doesn't work. 并返回它但它不起作用。

I would be very glad if someone with more knowledge could explain what is happening here detailed as possible. 如果有更多知识的人能够尽可能详细地解释这里发生的事情,我将非常高兴。 Many thanks. 非常感谢。

self:: is used to access static members of a class. self::用于访问类的静态成员。

So in this case, self::$application is used to cache the application object within JFactory to avoid multiple calls to JApplication::getInstance which is more expensive. 所以在这种情况下, self::$application用于在JFactory中缓存应用程序对象,以避免多次调用更昂贵的JApplication::getInstance

For more info on statics, see Static Keyword . 有关静态的更多信息,请参阅静态关键字

getApplication() - Returns a reference to the Global JApplication object. getApplication() - 返回对Global JApplication对象的引用。 Read more 阅读更多

self::$member for static members to be accessed. self::$member表示要访问的静态成员。

Here is an explanation as far I can understand. 这是我能理解的解释。

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)

What this does is if $application exist a function call is saved. 这样做的是,如果存在$ application,则保存函数调用。 If not get a new instance. 如果没有获得新实例。 Read more . 阅读更多 Hope this helps you out. 希望这可以帮助你。

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

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