Re: Class/functions question
| From: | M. Sokolewicz | Date: | Sun, 12 Feb 2006 09:52:51 +0000 |
| Subject: | Re: Class/functions question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-230264@lists.php.net to get a copy of this message | ||
Paul Goepfert wrote:
Hi all, I building a website with PHP. I am going to be using functions to deal with vaildation. I am putting all the methods in a class called Validation. My problem is I do not know how to call the function. Well I shouldn't say that. I know how to call functlions, I just just don't know how to do it in PHP. Let me put it this way. When I write programs in C++/Java I know that I have to do the following steps, create an object reference to the class use the object reference to access the methods. I am almost certain that it is the same thing in PHP. So this is what I did in my web page. <?php if (isset($submit)) { //validation code class Validation { function checkEmpty ($var) { if (empty($var)) { echo "YEAH It works!!"; } } } else {?> ..... <tr> <td align="left">First Name</td> <td align="left"> <span class="color">*</span><input type="text" name="name" size="20"> <?php $v = new Validation; $v->checkEmpty($_POST["name"]); ?> </td> </tr> ...... ... = rest of php/html code the else block holds the entire html code for the page Do I have the logic right? PaulYes and no. Yes, you're calling it correctly, and no because when submit is set, your php scipt will have a class called Validation, and no HTML is being sent to output. On the other hand, when submit is not set, you output a bunch of HTML, but never define your class, and thus will get a couple of errors from it. - tul