<?php
class DBFactory {
function __construct(){
return 'Need to echo';
}
}
$db = new DBFactory;
echo $db;
?>
Not works 🙁
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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
newkeyword 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
This is REALLY REALLY Stupid to do but as you wish to know how,