PHP: Can a constructor return a value?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.