Assigning a function's result to a variable within a PHP class? OOP Weirdness

前端 未结 3 1204
悲哀的现实
悲哀的现实 2020-12-06 20:43

I know you can assign a function\'s return value to a variable and use it, like this:

function standardModel()
{
    return \"Higgs Boson\";   
}

$nextBigTh         


        
3条回答
  •  醉话见心
    2020-12-06 20:46

    You can't assign default values to properties like that unless that value is of a constant data type (such as string, int...etc). Anything that essentially processes code (such as a function, even $_SESSION values) can't be assigned as a default value to a property. What you can do though is assign the property whatever value you want inside of a constructor.

    class test {
        private $test_priv_prop;
    
        public function __construct(){
            $this->test_priv_prop = $this->test_method();
        }
    
        public function test_method(){
            return "some value";
        }
    }
    

提交回复
热议问题