Static and Non-Static Calling in PHP

前端 未结 3 1023
不思量自难忘°
不思量自难忘° 2021-02-07 14:19

ok I have this code, that I\'m studying

 class scope{

    function printme(){
        return \"hello\";
    }

    public static function printme(){
        ret         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-07 14:37

    If the instance of your class is rarely needed, you can have the static method create an instance, call the non-static method and return the value.

    class Scope {
        public function mynonstatic() {
        }
    
        public static function mystatic() {
            $s = new Scope();
            return $s->mynonstatic();
        }
    }
    

    Remember that a static method is really just a global function with reduced scope. They are useful, but are should not be created without good reason.

提交回复
热议问题