Static methods in PHP

前端 未结 6 750
走了就别回头了
走了就别回头了 2020-12-15 04:38

Why in PHP you can access static method via instance of some class but not only via type name?

UPDATE: I\'m .net developer but i work with php developers too. Recent

6条回答
  •  忘掉有多难
    2020-12-15 04:59

    In PHP, while you're allowed to access the static method by referencing an instance of the class, you don't necessarily need to do so. For example, here is a class with a static function:

    class MyClass{
        public static function  MyFunction($param){
            $mynumber=param*2;
            return $mynumber;
    }
    

    You can access the static method just by the type name like this, but in this case you have to use the double colon (::), instead of "->".

    $result= MyClass::MyFunction(2);
    

    (Please note you can also access the static method via an instance of the class as well using "-->"). For more information: http://php.net/manual/en/language.oop5.static.php

提交回复
热议问题