Results 1 to 4 of 4

Thread: PHP Classes

  1. #1
    Roll Tide! mobilebadboy's Avatar
    Join Date
    Apr 2004
    Location
    Mobile, AL
    Posts
    428

    PHP Classes

    I'm trying to get into OOP and was checking classes out. I found what I figured would be a simple first choice, which is a form validation class. It works, but it's spitting out a warning message telling me it's missing an argument, which is what the class is supposed to be checking for.

    I know I could suppress it, but I'd rather know if anything can be done to fix it.

    Warning: Missing argument 1 for validate() in /myscript/ on line 82

    // The errored line
    function validate($theinput,$description = ''){
    My class:

    PHP Code:
    class Validate {

        var 
    $errors;
        function 
    validate($theinput,$description ''){
            if (
    trim($theinput) != "") {
                return 
    true;
            }else{
                
    $this->errors[] = $description;
                return 
    false;
            }
        }
        function 
    foundErrors() {
            if (
    count($this->errors) > 0){
                return 
    true;
            }else{
                return 
    false;
            }
        }
        function 
    listErrors($delim ' '){
            return 
    implode($delim,$this->errors);
        }


    Other code:

    PHP Code:
    $validator = new Validate();

    $validator->validate($_POST["user"],'Username');
    $validator->validate($_POST["pass"],'Password');
         if ( 
    $validator->foundErrors() ){
              echo 
    'The following fields were empty: <br />'.$validator->listErrors('<br />'); 
                
         }else{
              
    // rest of my code
         

    Shawn Kerr .com

  2. #2
    Registered
    Join Date
    Apr 2006
    Location
    Michigan
    Posts
    99
    You are using php 4 style classes. I would highly recommend using php 5, if you can. Your problem is that the name of the class and your function name are the same. This is a special case in php 4 called a constructor. The constructor is called when the class is instantiated. So, the line
    PHP Code:
    new validate() 
    actually calls the function validate() on the Validate class. Since you did not pass any parameters in the call, there is an error.

    It is most unfortunate that PHP reports the error line number as the declaration of the class and not the call.

    In PHP 5, there is a less confusing notation for declaring a constructor. All constructor methods use the name __construct. However, since there is still backward compatibility with 4, your case still causes the same error in php 4.

    Change the name of your class to validator to fix the problem.

  3. #3
    Registered
    Join Date
    Apr 2006
    Location
    Michigan
    Posts
    99
    I just ran this small test program:
    PHP Code:
    <?php
    function test($arg) { echo "talk like a pirate."; }
    test();
    ?>
    Which gives me the error message:

    Warning: Missing argument 1 for test(), called in /Users/jeff/- on line 3 and defined in /Users/jeff/- on line 2

    Which is much more informative. This was tested on php 5.2.2. Perhaps your error message from an older version of PHP is not so helpful? Another reason to upgrade.

  4. #4
    Roll Tide! mobilebadboy's Avatar
    Join Date
    Apr 2004
    Location
    Mobile, AL
    Posts
    428
    Thanks, for both the explanation and the easy fix. That did the trick and makes sense. I'm tired of coding stuff like if((!$_POST["user"] || !$_POST["pass"] ||...... that I want to learn how to do it different and better. Plus I should just learn OOP which I've put off for a while now.

    It's funny, I did something similar about a week ago. Assigned $cat = $_GET["cat"] at the beginning of a script, further in the script had a MySQL query set up as $cat = mysql_query("select.......") and then couldn't figure out why any queries following that one quit working. Drove me nuts for like 20 minutes, then I felt like a dummy once I realized it.

    I've thought often about updating to PHP5, but with so many script across so many domains, I keep putting it off in fear off a mass meltdown of my websites. Most of the scripts should survive, but it's still that "oh crap I have to stay up all night recoding..." thing in the back of my mind that gets me.
    Shawn Kerr .com

Similar Threads

  1. Review: Professional PHP Programming
    By Chris in forum Books
    Replies: 6
    Last Post: 07-17-2013, 05:26 AM
  2. Super Cheap PHP 5, RoundCube Webmail Linux Shared Hosting
    By hostingatoz in forum The Marketplace
    Replies: 0
    Last Post: 08-04-2007, 01:00 AM
  3. Replies: 0
    Last Post: 05-24-2007, 04:48 AM
  4. Using PHP to produce PHP really screws with the head.
    By KLB in forum Website Programming & Databases
    Replies: 8
    Last Post: 02-14-2007, 11:36 AM
  5. Replies: 0
    Last Post: 11-23-2005, 04:49 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •