Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7036577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:26:27+00:00 2026-05-28T01:26:27+00:00

EDIT: Please note that I am appending my original question with my actual question

  • 0

EDIT: Please note that I am appending my original question with my actual question due to realizing that I asked the wrong question (by forgetting what I actually meant to ask).

In PHP 5.4 (as of RC1), it is possible to access class methods on class instantiation.

In other words, as I understand it, it is now possible to do: $foo = new bar()->foobar(); which saves times and lines of code.

For my question, let’s look at the following example:

class foo {
    function __construct() {
    }

    function bar() {
        return 'world';
    }
}

$foobar = (new foo())->bar();
echo $foobar;

My question is, in such a case, when both the constructor and the method that is called upon initialization return a value, what is output by the final line (i.e. echo $foobar;)?

If I had to guess, since I haven’t actually had a chance to play around with 5.4 yet, the return from foo::bar() would overwrite the return from foo::__construct() because it is returned last. But I just wanted to check.


Okay, so the above question is not even logical (since __construct() should not return). What I really meant to ask is this:
Does $foobar in the above example contain the class instance or the return from $bar? If the latter, what sense does it make to use this if the class will not be available after instantiated? Wouldn’t it be better to just use a static method or call the method on its own line?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:26:27+00:00Added an answer on May 28, 2026 at 1:26 am

    First of all, you should never return anything from a constructor. Thats a basic rule. Now some of us don’t follow this because they like the concept of calling their constructor afterwards and receiving some input instead of nothing.

    But in the case of a good programming strategy, a constructor should be called only via the “new” keyword and shouldn’t return anything.

    And yes, in all cases, NEW CLASSNAME() returns the instanciated object in all cases 🙂

    Happy programming…

    <?php
    class myclass {
            public function __construct(){
                    return 'hello';
            }
    }
    
    $a = new myclass();
    var_dump($a);
    var_dump($a->__construct());
    

    Will output

    object(myclass)#1 (0) { } 
    string(5) "hello" 
    

    EDIT following question change

    My educated guess is that $foobar will contain “world” and thus echo it with your code.

    Same thing occurs in other languages such as in c#. You can instanciate an object call a method directly inline and lose the object only to get the return from the inline function called.

    var_dump(new foo()); //Will var_dump the object
    var_dump(new foo()->bar()); //Will var_dump "world"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: Please note that while I received an answer, I don't understand what's wrong
EDIT: Please note that this question is OUTDATED; RVM got way easier to use
EDIT: Please note that the app comes up just fine in Passenger/Apache2. Just not
Please note the Edit below for a lot more information, and a possible solution
**EDIT: There are several options below that would work. Please vote/comment according to your
Edit: From another question I provided an answer that has links to a lot
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
I earlier asked another question here However, I later realized that my requirements are
For whatever reason - my update panel is not updating. Now please note that
I'm trying to post on the page that I create. **Please note that this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.