views:

61

answers:

6
<?php
class DBFactory {  
     function __construct(){  
         return 'Need to echo';
                  }  
}  
$db = new DBFactory;  
echo $db;
?>

Not works :(

A: 

with the above code you're trying to echo the object not the contruct

Flakron Bytyqi
i know, but how to echo the contruct
Snoob
$db->__construct();
Flakron Bytyqi
+5  A: 

You cannot return anything from the constructor. You're already getting a new object back, you can't get another value on top of that and assign both to $db.

deceze
A: 
$db = new DBFactory();  

i think this "()" also be here

Richa
When there are no parameters passed, the parentheses are optional.
Dennis Haarbrink
the parentheses are just parameter requesters, nothing more.
RobertPitt
+2  A: 

The constructors should not return anything.

If you want to echo an object, you have to define how to form its string representation with the magic method __tostring:

class DBFactory {  
     function __tostring(){  
         return 'Need to echo';
     }  
}
$db = new DBFactory();
echo $db;
Artefacto
@RobertPitt What? Who said it did?
Artefacto
@RobertPitt It'll work the way the OP expects in his example though. :o) Probably for the wrong reasons though.
deceze
@deceze The OP didn't say he wanted to return anything on instantiation. In fact, he didn't say much. I just told him how he could get the result he was trying to accomplish.
Artefacto
@Artefacto You might want to direct that at Robert (who's gone), I'm already with you on that point. I'm just saying the OP may expect it to work for the wrong reasons.
deceze
`_toString` is probably what the OP is looking for (as far as it can be told). Why the downvote?
Pekka
A: 

Generally it's not possible to return a value in a constructor of a class. In this case, $db contains the instance of the class, not the return value.

You could build a separate function, and have that function return the value:

<?php
class DBFactory {  
      function toEcho() {
        return 'Need to echo';
      }
}  
$db = new DBFactory();  
echo $db->toEcho();
?>
TumbleCow
Ok, thanks all, i think i will do this way!
Snoob
I think @Artefacto's solution is better in an OO context, but that really depends on what exactly you want to do. @TumbleCow You could leave out the constructor entirely here. Also, you're looking for the `->` operator there.
deceze
Thanks, this is just me trying to be helpful while not having touched php for half a year. I agree that @Artefacto's solution is more elegant, but it's a bit easier to see what's going on here for a novice.For any real code, just use @Artefacto's solution. :)
TumbleCow
+5  A: 

I dont understand why your looking into OOP if your tryiung to return values on a constructor.

the whole point of OOP is to have objects that perform many tasks, if you want to return a string,array,resource then OOP is not for you.

__constructors are used to initiate code during the pre stages of the object initialization, witch allows you to execute code to prepare an object before the user can use it.

If you wish to use the __toString on objects then use it wisely, its main perpose is for a readability factor in objects, not storage etc. mainly used in error debugging.

When you create an object using the new keyword php's processor creates an object and assigns it to the memory, it then runs the construct but does not hold any returned values from it, after the constructor as reached its endppoint, the link for the object in the memory is returned to the variable you asked it to be. so in theory you can run $db->__construct() as its still a method, but only after the object is fully created.

just create a method to return a string like so

class DBFactory
{
     function whatAmI()
     {
         return 'I am DBFactory';
     }  
}
$MyOBJECT = new DBFactory;
echo $MyOBJECT->whatAmI();

This is REALLY REALLY Stupid to do but as you wish to know how,

class DBFactory{  
     function __construct()
     {
         return 'Need to echo';
     }
}

$db = new DBFactory();
echo $db->__construct();
RobertPitt