Is it possible to declare a method static and nonstatic in PHP?

后端 未结 5 1470
北荒
北荒 2020-12-01 09:04

Can I declare a method in an object as both a static and non-static method with the same name that calls the static method?

I want to create a class that has a stati

5条回答
  •  猫巷女王i
    2020-12-01 09:29

    No you can't have two methods with the same name. You could do basicly the same thing by renaming one of the methods. Renaming test::send("Hello World!"); to test::sendMessage("Hello World!"); would work. I would just create the a single send method with an optional text argument that changes how the method functions.

    public function send($text = false) {
        if (!$text) {
            $text = $this -> text;
        }
    
        // Send something
    }
    

    I courious as to why you need the static function at all.

提交回复
热议问题