jaikar Posted May 8, 2008 Share Posted May 8, 2008 hi there. here is the code <?php class test { function __construct() { return 'hello'; } } $d = new test; echo $d; ?> it prints object#1, anyhow i can understand the core concept, but what if when i have a situation where i will have to return a value in the constructor itself and how to grab that value? is there any way or i have to create a seperate method to grab the value? is it right to return values in the constructor? please advise... Thanks in advance... Link to comment https://forums.phpfreaks.com/topic/104720-solved-returning-string-from-__construct-not-working-why/ Share on other sites More sharing options...
The Little Guy Posted May 8, 2008 Share Posted May 8, 2008 Try this: <?php class test { function __construct() { return 'hello'; } } echo $d = new test(); ?> Link to comment https://forums.phpfreaks.com/topic/104720-solved-returning-string-from-__construct-not-working-why/#findComment-535980 Share on other sites More sharing options...
discomatt Posted May 8, 2008 Share Posted May 8, 2008 Because classes are objects, not strings. You're defining $d as an object, and the return value of a method within that object will not change that. Constructors aren't designed to return values. It's just code that's executed when a function is called. Your best bet is to use a public class variable, and verify success/failure/returns with that. To back it up with code <?php class test { public $out; public function __construct() { $this->out = 'hello'; } } $d = new test; echo $d->out; ?> Link to comment https://forums.phpfreaks.com/topic/104720-solved-returning-string-from-__construct-not-working-why/#findComment-535982 Share on other sites More sharing options...
The Little Guy Posted May 8, 2008 Share Posted May 8, 2008 You should do this, same as php.nets code: <?php class test { function __construct() { print 'hello'; } } $d = new test(); ?> Link to comment https://forums.phpfreaks.com/topic/104720-solved-returning-string-from-__construct-not-working-why/#findComment-535989 Share on other sites More sharing options...
PFMaBiSmAd Posted May 8, 2008 Share Posted May 8, 2008 The constructor does not and cannot return a value. It in fact returns nothing - void void __construct ( [mixed $args [, $...]] ) $d = new test(); returns the class object that is created. The purpose of a constructor is to perform - "any initialization that the object may need before it is used." (quote directly from the php manual.) Link to comment https://forums.phpfreaks.com/topic/104720-solved-returning-string-from-__construct-not-working-why/#findComment-535990 Share on other sites More sharing options...
jaikar Posted May 9, 2008 Author Share Posted May 9, 2008 thankyou all for explanation of the concept !! i think discomatt's idea will workout in my scenario. Thanks! Link to comment https://forums.phpfreaks.com/topic/104720-solved-returning-string-from-__construct-not-working-why/#findComment-536559 Share on other sites More sharing options...
jaikar Posted June 8, 2008 Author Share Posted June 8, 2008 though this is a very old post i just found the solution and thought of sharing ... using __toString as one of a method in a class returns string in the instantiation level itself.. but this works only in PHP 5.2. so no need to call a seperate method to get a value. hope it is usefull for someone class test { function __construct() { $this->value = "Hello World"; } function __toString() { return $this->value; } } echo new test; // prints "Hello World"; Link to comment https://forums.phpfreaks.com/topic/104720-solved-returning-string-from-__construct-not-working-why/#findComment-560283 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.