Can I set up a default method argument with class property in PHP?

前端 未结 4 1379
离开以前
离开以前 2020-12-18 21:19

I\'m using PHP 5.2.6. I want to have a default value for an argument in a method, but it seems I\'m getting a bit too clever.

The class property blnOverwrite

4条回答
  •  萌比男神i
    2020-12-18 22:11

    You must do it that way, afaik, or use a class constant since php 5.3 but of course its fixed and cant be changed later so i would definitely go with your own solution:

    class foo{
        const constant = 'bar';
    
        public function place($path, $overwrite = self::constant ) { 
            die('bla' . $overwrite);
        }
    }
    
    $bar = new foo();
    $bar->place('baz');
    

提交回复
热议问题