Jump to content

Question on constructors


j.smith1981

Recommended Posts

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.
    }
  }
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.