2

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/… Commented 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 itself Commented 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. Commented 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. Commented 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) Commented Feb 13, 2015 at 17:43

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.