j.smith1981 Posted December 2, 2010 Share Posted December 2, 2010 I have a question about converting a procedural version of a contact form. Just with to html elements say the contact email address and message. If say the user was not to put in any input into either of the 2 fields or worse both, would I need to put an if control statement inside the constructor and make an our put (if so would I be able to call another method to display an error message?). Or would I have to make my own custom constructor, then call that if both fields have some data in them? Just for my own theory, just wanted to try and build up my own ecommerce solution at some point but wanted to handle some sort of dynamic Input and aim to use databases within that (means I would prefer to use OOP), but wanted to start off with the simple stuff so it really makes sense to me. Thanks for reading and I look forward to any responses, Jeremy. Quote Link to comment https://forums.phpfreaks.com/topic/220456-question-on-constructors/ Share on other sites More sharing options...
gizmola Posted December 2, 2010 Share Posted December 2, 2010 A constructor is simply a function that gets called when an object is instantiated with new. You don't call a constructor -- it gets called automatically. Quote Link to comment https://forums.phpfreaks.com/topic/220456-question-on-constructors/#findComment-1142198 Share on other sites More sharing options...
j.smith1981 Posted December 2, 2010 Author Share Posted December 2, 2010 Sorry I know that, just I dont think you read my post correctly or I didnt explain more clearly. Do I put an if statement into a constructor to work out if a user has entered any details into a form? Or should I put an if statement asking if the form has anything in it and then construct it? (obviously this one being a custom constructor). Quote Link to comment https://forums.phpfreaks.com/topic/220456-question-on-constructors/#findComment-1142212 Share on other sites More sharing options...
gizmola Posted December 3, 2010 Share Posted December 3, 2010 We don't know what your class structure is so there's no great way to give you advice. With that said, a common technique is to pass in optional input using an optional parameter: So then you can do: $frm = new myForm; or $frm = new myForm($_POST); class myForm { function __construct($post = NULL) { if ($post === NULL) { // No post data } else { // post data was passed. } } } Quote Link to comment https://forums.phpfreaks.com/topic/220456-question-on-constructors/#findComment-1142435 Share on other sites More sharing options...
vicodin Posted December 3, 2010 Share Posted December 3, 2010 You don't have to put it in the constructor. you can put it anywhere you want to. For example lets say this form is emailing someone with the info that was provided by the user. You could do it in the constructor and catch the empty field there but as long as you do it anytime before sending the email your fine. Quote Link to comment https://forums.phpfreaks.com/topic/220456-question-on-constructors/#findComment-1142476 Share on other sites More sharing options...
j.smith1981 Posted December 3, 2010 Author Share Posted December 3, 2010 Ah right so I would just call the custom function (i.e. not the __constructor), but call say sendMessage holding all the vars the user has entered with regards to html elements used in the class then? Will give this a go, have actually posted something of what I have done with regards to this and try that, wonderful! Thanks ever so much for the tips, really appreciate it, suppose its up to the developer themselves but is that the most logical way of doing it the above in theory? Thanks so far and I appreciate any replies in advance, Jez. Quote Link to comment https://forums.phpfreaks.com/topic/220456-question-on-constructors/#findComment-1142629 Share on other sites More sharing options...
gizmola Posted December 3, 2010 Share Posted December 3, 2010 Yes it's definately a design question. One thing to consider is what sort of interface or api are you trying to achieve for yourself. Whether or not something should be passed as a parameter in the constructor is up to you. Also it's important to keep in mind that you can call methods from other methods. So if you had a method like: myForm->loadPost($post); You can always optionally call this from inside your constructor using: $this->loadPost($post); From my point of view, if I was frequently going to want to create a form object and in doing so needed access to the post data (to redisplay with error messages and previously entered data, for example) then I'd probably build that option into the constructor, just so I could avoid having to explicitly make the method call, but there's no major difference other than convenience in the api. Years ago I wrote a tutorial and the example I used was a timer class. The class let you create timer objects and they had a timer->start() and timer->stop() param. In the constructor i provided an option to call $this->start() when the timer was created, because a lot of times that's what you would want to do --- make the timer and have it start timing immediately. Quote Link to comment https://forums.phpfreaks.com/topic/220456-question-on-constructors/#findComment-1142758 Share on other sites More sharing options...
j.smith1981 Posted December 6, 2010 Author Share Posted December 6, 2010 Ah thanks for that, will definately keep that in mind. Its really good that I am learning not really advaned parts of OOP but thats just me I like to really get into the bare bones of it, start small, understand it and grow bigger, then appreciate what things are the way they are. Thanks again, Jez. Quote Link to comment https://forums.phpfreaks.com/topic/220456-question-on-constructors/#findComment-1143570 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.