How can I write a method so that the returned value is an object?

Source: Internet
Author: User
How can I write a method so that the returned value is an object? how can I write a method so that the returned value is an object ??, Just return. PHPcodeclassmyObject {// ......} functionreturnObject () {$ mo = new $ myObje how can I write a method so that the returned value is an object?
How can I write a method to make it return an object ??

------ Solution --------------------
Simply return
------ Solution --------------------
PHP code
Class myObject {//...} function returnObject () {$ mo = new $ myObject (); return $ mo ;}
------ Solution --------------------
The one on the third floor is correct.
If it is a member function in a class, the returned pseudo variable $ this is to return an object (it itself)
------ Solution --------------------
In returnObject, the myObject class is instantiated in new. And return. What does it mean?



Discussion

Can I provide more details?
A little unintelligible.
Reference:

PHP code
Class myObject {
//......
}

Function returnObject (){
$ Mo = new $ myObject ();
Return $ mo;
}

------ Solution --------------------
C # code

Class MyClass
{
......
}

Public MyClass ReturnObject (MyClass myClass)
{
MyClass a = new MyClass ();
If (null! = MyClass)
{
A = myClass;
}
Return;
}
------ Solution --------------------
See the 4th floor


Discussion

Yes. I executed a method to return data of the string type.
This data is also used elsewhere, so I want to return an object. In this way, I can write similar statements in this way.

$ A-> $ B ()-> $ c ()

I just thought it was more concise. That's why we have this problem. Reference:

Can I provide more details?
A little unintelligible.
Reference:
......

------ Solution --------------------
Return $ this-> $;

What is $ this-> $?

Hey, you have poor basic skills.
------ Solution --------------------
Discussion

Class myObject {

Public function a ($ ){

Return $ this-> $;
}

Public function B ($ B ){

Echo $ B;
}
}
$ C = new myObject ();

$ C-> a ('1aaaaa')-> B ();

Why not? no output ?? Reference helloyou0 on the 10th floor ......

------ Solution --------------------
Fix It
In this example, Method B does not need to return an object because it is used as the end
In this example, if Method B is followed by Method a, an error occurs.
------ Solution --------------------
I did not find a suitable example after 30 seconds,
I had to do it myself.

Class {
Private $ name = '';
Private $ belly = array ();

Public function _ construct ($ name ){
$ This-> name = $ name;
}

Public function ($ delicious ){
$ This-> belly [] = $ delicious food;
Return $ this;
}

Public function (){
$ Dongdong = array_pop ($ this-> belly );
If (! Empty ($ )){
Echo $ this-> name. 'spit'. $ this-> Dongdong. "\ n ";
}
Return $ this;
}

}

$ I = new ('helloyou0 ');
$ Me-> eat ('Greens ')-> vomit ()-> eat ('chicken')-> eat ('Duck ')-> vomit () -> vomit ();



------ Solution --------------------
Do you want this?




Class laaaaa {
Public $ B;
Public function B ($ B ){
$ This-> B = $ B;
Echo $ this-> B;
}
}

Class myObject {

Public $ laaaaa;

Public function a ($ ){

Return $ this-> $ a = new $ ();
}


}
$ C = new myObject ();


$ C-> a ('laaaa')-> B ('= _ = ');

?>


Discussion

Indicates the object of the returned string. reference:

Return $ this-> $;

What is $ this-> $?

Hey, you have poor basic skills.

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.