PHP类问题

发布于 2024-09-28 07:25:34 字数 737 浏览 4 评论 0原文

class Gdn {

   const AliasDispatcher = 'Dispatcher';

   protected static $_Factory = NULL;

   public static function Dispatcher() {

      $Result = self::Factory(self::AliasDispatcher);

      return $Result;
   }

   public static function Factory($Alias = FALSE) {

      if ($Alias === FALSE)

         return self::$_Factory;


      // Get the arguments to pass to the factory.
      //$Args = array($Arg1, $Arg2, $Arg3, $Arg4, $Arg5);

      $Args = func_get_args();

      array_shift($Args);

      return self::$_Factory->Factory($Alias, $Args);
   }

}

如果我像 $Dispatcher = Gdn::Dispatcher(); 这样调用 Dispatcher(),返回 self::$_Factory->Factory($Alias, $Args);< /代码> 是什么意思?

class Gdn {

   const AliasDispatcher = 'Dispatcher';

   protected static $_Factory = NULL;

   public static function Dispatcher() {

      $Result = self::Factory(self::AliasDispatcher);

      return $Result;
   }

   public static function Factory($Alias = FALSE) {

      if ($Alias === FALSE)

         return self::$_Factory;


      // Get the arguments to pass to the factory.
      //$Args = array($Arg1, $Arg2, $Arg3, $Arg4, $Arg5);

      $Args = func_get_args();

      array_shift($Args);

      return self::$_Factory->Factory($Alias, $Args);
   }

}

If I call the Dispatcher() like $Dispatcher = Gdn::Dispatcher();, what does return self::$_Factory->Factory($Alias, $Args); mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玻璃人 2024-10-05 07:25:34

这意味着 Dispatcher() 返回一个对象,并且该对象是 Factory() 创建的对象的副本。

It means Dispatcher() is returning an object, and that object is a copy of something created by Factory().

夕嗳→ 2024-10-05 07:25:34

self:: 表示您正在引用当前对象的类
由于工厂是一个递归函数,它将不断调用自身,直到用完参数,然后返回当前工厂类中设置的工厂。

如果你愿意:
“blah->Factory('test1','test2',test3','test4')” 它的运行方式如下:

blah->factory('test1','test2','test3','test4')
    blah->$_Factory->Factory('test1',array('test2','test3','test4'))
        blah->$_Factory->Factory(array('test2','test3','test4'));
            blah->$_Factory->Factory();
           // oh hey, i dont have any arguments, replace it with my default argument 'false' and thus return the factory
            return self::$_Factory;

我不知道为什么你会想要它,但这就是它的作用

self:: means you are refering to the class of the current object
since factory is a recursive function it will keep calling itself until it runs out of arguments and then returns the factory that is set in the current factory class.

if you would do:
"blah->Factory('test1','test2',test3','test4')" it would run like:

blah->factory('test1','test2','test3','test4')
    blah->$_Factory->Factory('test1',array('test2','test3','test4'))
        blah->$_Factory->Factory(array('test2','test3','test4'));
            blah->$_Factory->Factory();
           // oh hey, i dont have any arguments, replace it with my default argument 'false' and thus return the factory
            return self::$_Factory;

i dont know WHY you would want it, but this is what it does

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文