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

Why is this still 2 after using the constructor?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-10-19 10:40:52
Original
1131 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
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template