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

Why is this still 2 after using the constructor?

WBOY
Release: 2016-10-19 10:40:52
Original
1087 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

This one is missing $this->

<code> $this->age=$this->age+3;</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template