Home  >  Article  >  Backend Development  >  Why is this still 2 after using the constructor?

Why is this still 2 after using the constructor?

WBOY
WBOYOriginal
2016-10-19 10:40:521077browse

Why is the result 2 instead of 5?

<code>class a{
    public $age=2;
    public function __constrator(){
       $this->age=$age+3;
    }
    
}

$k=new a();
echo $k->age;</code>

Reply content:

Why is the result 2 instead of 5?

<code>class a{
    public $age=2;
    public function __constrator(){
       $this->age=$age+3;
    }
    
}

$k=new a();
echo $k->age;</code>

It made me laugh __constrator became __construct and $this->age=$this->age+3;

<code>class a{
    public $age=2;
    public function __constrator(){
       $this->age=$this->age+3; // 这里
    } 
    
}

$k=new a();
echo $k->age;</code>

This one is missing $this->

<code> $this->age=$this->age+3;</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn