Poor Foundation. Ask a beginner question about php class calls.

Source: Internet
Author: User
Poor Foundation. There is a Test for a preliminary question about php class calls. class. php class {code ...} call the following {code ...} NO content exists in echo. $ content is already an object. Call the following {code ...} output "My name is big, 40 years old, and my job is php programming... poor Foundation. Ask a beginner question about php class calls.

There is a Test. class. php class


  Name = $ name; $ this-> age = $ age; $ this-> work = $ work; $ this-> do_php ();} public function do_php () {$ content = "my name is ". $ this-> name. ", already ". $ this-> age. "My job is ". $ this-> work; return $ content ;}}?>

The call is as follows:

$ Content = new \ Test ('Big ', '40', 'php programming'); echo $ content;

NO content exists in echo. $ content is already an object.

In the following example

$ Content = new \ Test ('Big ', '40', 'php programming'); echo $ content-> do_php ();

Output "My name is big, 40 years old, and my job is php programming"

For more information, see $ content = new \ Test ('Big data', '40', 'php programming '). Directly echo the content.

Reply content:

Poor Foundation. Ask a beginner question about php class calls.

There is a Test. class. php class


  Name = $ name; $ this-> age = $ age; $ this-> work = $ work; $ this-> do_php ();} public function do_php () {$ content = "my name is ". $ this-> name. ", already ". $ this-> age. "My job is ". $ this-> work; return $ content ;}}?>

The call is as follows:

$ Content = new \ Test ('Big ', '40', 'php programming'); echo $ content;

NO content exists in echo. $ content is already an object.

In the following example

$ Content = new \ Test ('Big ', '40', 'php programming'); echo $ content-> do_php ();

Output "My name is big, 40 years old, and my job is php programming"

For more information, see $ content = new \ Test ('Big data', '40', 'php programming '). Directly echo the content.

In my opinion, the only answer to @ P sauce is that @ newborn is using other methods and does not match the meaning of the question. As for those returning in the constructor, it is too unreliable.
New is used to generate class instances. The instance construction method is to call the constructor, but the returned value of the constructor is meaningless because new is not a function, it does not regard the return value of the constructor as its own return value.
_ String is one of the magic methods that PHP class can use. In addition, there are _ sleep, _ get, _ set, and so on. _ String indicates the processing to be performed when the instance of the object needs to be converted to the string type. Therefore, the correct example is:

Class A {public function do () {return 'your conent';} public function _ string () {return $ this-> do ();}} $ obj = new A (); echo $ obj; // at this time, the type conversion of the object is actually triggered, and the __string () method is called.

Be sure to remember: when you use the new command to get the instance of an object, there are only two results. One is to get the object, and the other is to create an exception that fails. Never get a string. If you only want to get a string, 1) Declare the function. 2) define constants.

Supplement: Some people put forward the method of writing echo statements directly in functions. Do not do this. This is a bad habit and prevents micro-duing. In the function, echo, print, var_dump, and so on are all used during debugging. The final code cannot appear. Because you cannot determine who will call your function, embedded echo will surprise the caller.

Add__toStringFunction

The new object cannot directly return a string, and all objects are returned;
I understand what you mean. If you want to return the result directly when you want to get new, you can use the static method instead of the do_php ().

Solution 1:

  Name = $ name; $ this-> age = $ age; $ this-> work = $ work;} public function do_php () {$ content = "my name is ". $ this-> name. ", already ". $ this-> age. "My job is ". $ this-> work; return $ content; }}$ c = Test: g ('zhang san', 42, 'pupp')-> do_php (); echo $ c;
Solution 2

  Do_php ();} public function _ construct ($ name, $ age, $ work) {$ this-> name = $ name; $ this-> age = $ age; $ this-> work = $ work;} public function do_php () {$ content = "my name is ". $ this-> name. ", already ". $ this-> age. "My job is ". $ this-> work; return $ content; }}$ c = Test: g ('zhang san', 42, 'pupp'); echo $ c;

Hope to adopt

Note: repeated calls to the Test class will instantiate many objects in the memory. If optimization is required, optimize the g method.

static function g($name, $age, $work)    {        static $instance;        if (!isset($instance)) {            $instance = new Test($name, $age, $work);        }        return $instance;    }

I am also a little white, so you can try?

  public function __construct($name,$age,$work){    $this->name=$name;    $this->age=$age;    $this->work=$work;    return $this->do_php();  }  

Output with var_dump

Hello:
1. The first method of the landlord is to use echo to output objects, which is not allowed. Echo is an output function, which is limited to output strings.
The following error is reported:
Catchable fatal error: Object of class Test cocould not be converted to string in/Users/baidu/wwwroot/learn/object. php on line 32

2. The second method is correct, but the result is blank because the return result is not output. The following two solutions can be used for output. (Echo can be placed in two locations)

Class Test {private $ name; private $ age; private $ work; public function _ construct ($ name, $ age, $ work) {$ this-> name = $ name; $ this-> age = $ age; $ this-> work = $ work; $ this-> do_php ();} public function do_php () {$ content = "my name is ". $ this-> name. ", already ". $ this-> age. "My job is ". $ this-> work; echo $ content ;}$ test = new \ Test ('luboot', '40', 'php search ');
Class Test {private $ name; private $ age; private $ work; public function _ construct ($ name, $ age, $ work) {$ this-> name = $ name; $ this-> age = $ age; $ this-> work = $ work; echo $ this-> do_php ();} public function do_php () {$ content = "my name is ". $ this-> name. ", already ". $ this-> age. "My job is ". $ this-> work; return $ content ;}$ test = new \ Test ('luboot', '40', 'php search ');

Although you call do_php during _ construct

The do_php method also returns characters.
But no return is used when do_php is called in _ construct.
Therefore
......

  public function __construct($name,$age,$work){    $this->name=$name;    $this->age=$age;    $this->work=$work;    return $this->do_php();  }  

_ Tostring direct echo object

_ Condtructor: Put echo in it and print the content directly with a new object.

Class Test {

Private $ name;
Private $ age;
Private $ work;

Public function _ construct ($ name, $ age, $ work ){

$this->name=$name;$this->age=$age;$this->work=$work;$this->content = $this->do_php();

}

Public function do_php (){

$ Content = "my name is ". $ this-> name. ", already ". $ this-> age. "My job is ". $ this-> work; return $ content;

}

}
$ Content = new \ Test ('Big ', '40', 'php programming ');
Var_dump ($ content-> content );
Die ();

Thank you very much for your reply!

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.