Object oriented programming is a so high abstract level that sometimes I must to put in doubt my own conviction about the class I'm just creating. Well, I have made some research over some books and internet sites about the question if it is a good practice to use return inside __construct function. So far, I use __construct only to initialize objects. Is there a good site where I could find pros and cons about this, and showing examples? Now I am using php.
5
-
That question seems to be similiar to this one if I understand it correctly : stackoverflow.com/questions/6849572/…Tacticus– Tacticus2015-02-13 17:26:19 +00:00Commented Feb 13, 2015 at 17:26
-
There is no point in using return inside a constructor, as the only item passed back to the calling script is the instantiated object itselfMark Baker– Mark Baker2015-02-13 17:30:24 +00:00Commented Feb 13, 2015 at 17:30
-
You don't return anything from a __constructor. You should use a function if that's the result you need/want.Dave– Dave2015-02-13 17:31:27 +00:00Commented Feb 13, 2015 at 17:31
-
Yes it could be. But in fact I would like to find some more deep information about the use (pros and cons). Some book says only initialize but in the same book we can find some return.IgorAlves– IgorAlves2015-02-13 17:31:38 +00:00Commented Feb 13, 2015 at 17:31
-
If you're saying that some books and internet sites say you should return something from in a constructor, then please cite references (so we can steer people clear of books that spread disinformation in future)Mark Baker– Mark Baker2015-02-13 17:43:53 +00:00Commented Feb 13, 2015 at 17:43
Add a comment
|
1 Answer
Constructor only purpose is to create an instance of a class.
You should think of constructor a way to prepare the object for use, nothing is returned, all you do is create an instance of a class.
2 Comments
IgorAlves
This is what I think and understand about the idea of constructor. But sometimes we find inside some programming for professional books, some examples where the author returns something inside construct method. Is there some good practice manual or something like that of how to properly create constructs and explain why?
meda
Like I said the purpose is to prepare/set some settings on the class, not to get something back. I have never seen a constructor returning a value, for best practice You can read msdn.microsoft.com/en-us/library/vstudio/…