user2328931
user2328931

Reputation: 39

how to get returned data from constructor?

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?

Upvotes: 0

Views: 199

Answers (7)

CodeAngry
CodeAngry

Reputation: 12985

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.

Upvotes: -1

vascowhite
vascowhite

Reputation: 18440

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();

Upvotes: 1

user1864610
user1864610

Reputation:

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();

Upvotes: 1

Orangepill
Orangepill

Reputation: 24645

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

Upvotes: 1

Andy G
Andy G

Reputation: 19367

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

Upvotes: 1

luk2302
luk2302

Reputation: 57124

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();

Upvotes: 1

Baba
Baba

Reputation: 95111

Use __toString

class example {
    function __construct() {
    }

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

$forex = new example();
echo $forex; //something 

Upvotes: 4

Related Questions