Which is the better approach to initialize php properties?

前端 未结 5 1540
眼角桃花
眼角桃花 2021-02-19 11:35

Here are two way to initialize class variables.

1st Method

class Test {
    private $var1;
    private $var2;

    public function Test($var1,$var1) {
         


        
5条回答
  •  你的背包
    2021-02-19 12:16

    What about this

    class A{
    
        public $x;
        public $y;
        function A($var1=10,$var2=15){   //the default value for the class
            $this->x=$var1;  
            $this->y=$var2;
    
        }
    }
    
    $object_of_A= new A(20,30);  //if you do not want to change the default value then
                                   //pass no arguments
    

提交回复
热议问题