0

Shortly, title is what I want to learn.

class example {
    function __construct() {
        return 'something';
    }
}
$forex = new example();
// then?

I want to echo something, but how?

I know I can define a variable and I can reach that outside of class but my purpose of writing this question is just learning. Is there any way?

3
  • 3
    you cannot i guess, a contructor returns the object that you create by calling it. Commented Jun 16, 2013 at 19:48
  • 1
    A constructor is a subroutine which, by definition, is responsible for initializing state. In languages with new this typically means that the call evaluates to a newly-allocated area of memory, therefore a return value would be pointless. Why do you want to return something from a constructor? Commented Jun 16, 2013 at 19:50
  • possible duplicate of Constructor returning value? Commented Jun 16, 2013 at 20:08

7 Answers 7

4

Use __toString

class example {
    function __construct() {
    }

    function __toString() {
        return 'something';
    }
}

$forex = new example();
echo $forex; //something 
Sign up to request clarification or add additional context in comments.

2 Comments

I have no idea why somebody would down vote this, seems like a good answer to me. It is certainly one of the options that came to my mind.
@vascowhite I didn't but maybe they did because it's improper. Use a function if you just need output. Why instantiate an object for that? Plus you should use output buffering with ob_start() in __toString for more complex output and return the ob_get_clean().
1

You cannot, a contructor is a function that creates the object itself and instantiates it. You have to put the code to return something in an function outside the contructor and call it afterwards.

Like this:

class example {
  function __construct() {
    //setup
  }

  function init() {
    return 'something';
  }
}
$forex = new example();
echo $forex->init();

Comments

1

We cannot return a value from a constructor. Internally, it returns a reference to the newly created object.

Comments

1

Constructors don't return anything. If the goal is to echo something during the construction process then simply add echo "something"; to the body of the constuctor

Comments

1

A constructor returns a new object.Add a method to return something and echo the output from that:

class example {
private $data;

function __construct() {
    // something for the constructor to do.
    // this could have been done in the property declaration above
    // in which case the constructor becomes redundant in this example.

    $this->data= 'something';
}
function getSomething() {
  return $this->data;
}
}
$forex = new example();
// then?
echo $forex->getSomething();

Comments

1

An alternative to Baba's answer would be to call the constructor and the required function in one line:-

class example {
    function __construct() {
    }

    function doSomething() {
        return 'something';
    }
}

$forex = (new Example())->doSomething();

Comments

-1

A constructors job is to setup internal properties which can be later accessed or echoed. Or to throw exceptions and prevent construction if certain requirements are not met. It SHOULD NOT echo something. Echoing is done later.

class example {
    public $time = null;
    function __construct() {
        $this->time = time();
    }
}

$ex = new example();
echo strftime('%Y-%m-%d %H:%M:%S', $ex->time);

I don't get it why responders encourage bad practices here (echoing in constructor). Teach the poster the right way. If you need echoing, use a damn function. Why construct an object if you just need some output after you process something? The whole purpose of the object is to hold properties later usable or multiple methods that work together and access those properties. And there are other reasons but way too advanced for the current context.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.