I know the question I asked is very basic, but this is where I have questions
<code class="php">class car{ private $name; private $num; public function __construct($name,$num){ $this->name=$name; $this->num=$nau; } }</code>
Why do you write it like this? $this->name=$name;
Why initialize global variables;
I know the question I asked is very basic, but this is where I have questions
<code class="php">class car{ private $name; private $num; public function __construct($name,$num){ $this->name=$name; $this->num=$nau; } }</code>
Why do you write it like this? $this->name=$name;
Why initialize global variables;
<code> 这个问题有点像多态: class User { public $user_name; public $age; public function __construct($user_name,$age) { $this->user_name = $user_name; $this->age = $age; } } $obj1 = new User('张三',28); //用户1 $obj2 = new User('张思','22'); //用户2 //看到没有?构造的好处,你一个用户类,不用改动任何代码就能实现多态,每次new的时候传不同的参数 </code>
There is no why, no need to question, it’s just that you write it like this when you use it.
1- There is no requirement that a class must have a constructor
2- There is also no requirement that attributes must be initialized during construction
For example
<code>class Car { private $color = 'Blue'; private $band = 'Audi'; public function set($attr, $value) { $this->$attr = $value; return $this; } } $car = new Car(); $car->set('color', 'red')->set('band', 'BMW');</code>
There is no difference between writing this way and how you write it, it just needs to be changed accordingly when using it. So, the answer to your question is: the person who wrote the code thought it was good, and then they wrote it.
Otherwise, when will it be initialized?
$name
is just a local variable and cannot be accessed after __construct
is removed.
Thanks for the invitation. . But I’m not very clear about anything too specific
__construct is the construction method
It is the method that is executed first when instantiating a class
The two parameters of __construct are passed in during instantiation
For examplenew car ($name,$num);
After instantiation like this, the $name and $num variables become private variables of the car class
Convenient to call by the class itself. . .
As for $this, it is equivalent to the class itself. $this-> can then call the methods or properties of the class itself
It is better to read the official manual first
http://php.net/manual/zh/language.oop5.basic.php
Usually in the constructor, initialize member variables and instantiate classes used by many member methods (it is clearer to write code this way, but some classes are only used once, and it is not recommended to instantiate them in the constructor, which affects program performance)
Well, actually when I first started using php
, I didn’t know why I used it this way. There is no such thing as a low-level question, just ask if you are not sure. The function of the constructor is that every time you create a new object, you will first call this method. In fact, to put it bluntly, it is the function of initialization. Hope it helps you