【问题标题】:difference between :: and -> in calling class's function in php:: 和 -> 在 php 中调用类函数的区别
【发布时间】:2010-07-23 21:21:30
【问题描述】:

我见过用 :: 或 -> 从 php 类调用的函数。

例如:

$classinstance::function 或者 $classinstance->函数

有什么区别?

【问题讨论】:

    标签: php class function


    【解决方案1】:

    :: 用于scope resolution,访问(通常)静态 方法、变量或常量,而-> 用于特定对象实例上的invoking object methods or accessing object properties

    换句话说,典型的语法是……

    ClassName::MemberName
    

    对比...

    $Instance->MemberName
    

    在您看到$variable::MemberName 的极少数情况下,实际情况是$variable 的内容被视为类名,因此$var='Foo'; $var::Bar 等同于@987654332 @。

    http://www.php.net/manual/en/language.oop5.basic.php

    http://www.php.net/manual/language.oop5.paamayim-nekudotayim.php

    【讨论】:

      【解决方案2】:

      :: 语法意味着您正在调用static 方法。而-> 是非静态的。

      MyClass{
      
        public function myFun(){  
        }
      
        public static function myStaticFun(){
        }
      
      }
      
      $obj = new MyClass();
      
      // Notice how the two methods must be called using different syntax
      $obj->myFun();
      MyClass::myStaticFun();
      

      【讨论】:

      • 严格来说这不是真的。您可以使用-> 调用静态方法(尽管您不应该这样做,但它甚至不会通过严格的警告)并且您可以使用:: 调用实例方法(您必须这样做,如parent::parentImplementationOfThisMethods() 或你真的不应该像$obj::myFun())。
      【解决方案3】:

      例子:

      class FooBar {
          public function sayHi() { echo 'Hi!'; }
          public /* --> */ static /* <-- */ function sayHallo() { echo 'Hallo!'; }
      }
      
      // object call (needs an instance, $foobar here)
      $foobar = new FooBar;
      $foobar->sayHi();
      
      // static class call, no instance required
      FooBar::sayHallo(); // notice I use the plain classname here, not $foobar!
      
      // As of PHP 5.3 you can write:
      $nameOfClass = 'FooBar'; // now I store the classname in a variable
      $nameOfClass::sayHallo(); // and call it statically
      
      $foobar::sayHallo(); // This will not work, because $foobar is an class *instance*, not a class *name*
      

      【讨论】:

        【解决方案4】:

        ::function 用于静态函数,实际上应该用作:

        class::function() 而不是你建议的 $instance::function()。

        你也可以使用

        类::函数()

        在子类中引用父类的方法。

        【讨论】:

          【解决方案5】:

          :: 通常用于调用static 方法或Class Constants。 (换句话说,您不需要使用new 实例化对象)才能使用该方法。 -&gt; 是你已经实例化了一个对象的时候。

          例如:

          Validation::CompareValues($val1, $val2);
          
          $validation = new Validation;
          $validation->CompareValues($val1, $val2);
          

          请注意,您尝试用作静态(或使用::)的任何方法在定义它时都必须使用静态关键字。阅读我在这篇文章中链接到的各种 PHP.net 文档页面。

          【讨论】:

            【解决方案6】:

            使用::,您可以访问的常量、属性或方法;变量和方法需要声明为静态,否则它们确实属于实例而不属于类。

            使用-&gt;,您可以访问类实例的属性或方法。

            【讨论】:

              猜你喜欢
              • 2015-11-09
              • 2021-04-19
              • 1970-01-01
              • 2013-07-20
              • 1970-01-01
              • 2012-03-10
              • 1970-01-01
              • 2014-03-21
              • 1970-01-01
              相关资源
              最近更新 更多