PHP: Can a constructor return a value?
Class a{ function construct () { return ' abc '; }}
May I?
Looks like it's not going to work. 、、、 forgot, no idea.
You can write that, but new will still be the object of this class.
Why do you use it so much??
You want to have a function that returns ABC just need to write another call once.
No!
The function of a constructor is primarily used to define the state of initialization when the class's object is created. It has no return value and cannot be decorated with void.
Constructors are data that is used to initialize data only in the
depend on, this question still have to ask, really do not know to write a test example to see not to finish
It is not possible to get or return some values in the constructor, but writing a function with the same name as the class name can accomplish this.
Class abc{public $var 1; Public $var 2; Private Function ABC ($var 1, $var 2) { $this->var1= $var 1; $this->var2= $var 2; }} .... $ABC =new ABC (123, ' abc '); Var_dump ($ABC);
However, writing a function with the same name as the class name can achieve this goal
God horse means???
A function with the same name as the class name is not a constructor??
Have you ever seen a new object return a value of another type?? New returns an object, so the constructor is an object regardless of what you manually return.