Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Angry OOP function array help

    Hi all,

    This is a seriously trivial issue which likely has something to do with variable scope from what I've assertained but any help would be greatly appreciated. Basically I'm in the midst of my first delve into object oriented programming (about time I resurrected myself to escape old school DRY coding).

    Things were going fine until I encountered a problem passing an array (of errors) back from a function.

    As it stands I perform validation inside a function with each adding to an errors array e.g. (errors[] = "please enter username"). All fine there. Post-validation I can display the array from WITHIN the function via print_r($errors) and it displays at the top of the corresponding page.

    However, any attempt to access this array from the recipient page results in an empty array or a call to an undefined. Should return $errors not pass the variable back outside the function?

    Hope this makes sense - thanks in advance for any help guys

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    Cannot possibly help you with your code without seeing the offending code.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post
    PHP Code:
        # Top level validation of json encoded submission
        
    public function validate_registration($json) {
            
    $fields json_decode($jsontrue);
            
            if(empty(
    $fields['username']) || empty($fields['password']) || empty($fields['email']) || empty($fields['forename']) || empty($fields['surname'])) {
                
    $errors[] = 'All fields are required.';
            } 
            else {
                if (
    $this->user_exists($fields['username']) === true) {
                    
    $errors[] = 'That username already exists';
                }
                if(!
    ctype_alnum($fields['username'])){
                    
    $errors[] = 'Please enter a username with only alphabetic and numerical characters';    
                }
                if (
    strlen($fields['password']) <6){
                    
    $errors[] = 'Your password must be at least 6 characters';
                } else if (
    strlen($fields['password']) >18){
                    
    $errors[] = 'Your password cannot be more than 18 characters long';
                } else if (
    $fields['password'] !== $fields['password2']) {
                    
    $errors[] = 'Your passwords do not match';
                }
                if (
    filter_var($fields['email'], FILTER_VALIDATE_EMAIL) === false) {
                    
    $errors[] = 'Please enter a valid email address';
                }else if (
    $this->email_exists($fields['email']) === true) {
                    
    $errors[] = 'That email address has already been registered.';
                }
            }
            
            
    print_r($errors);
            
            
    # If there are no errors proceed to database insertion
            
    if(empty($errors) === true){
                
    $title        $fields['title'];
                
    $forename    htmlentities($fields['forename']);
                
    $surname    htmlentities($fields['surname']);
                
    $username     htmlentities($fields['username']);
                
    $password     $fields['password'];
                
    $email         htmlentities($fields['email']);
                
    $dobday        $fields['day'];
                
    $dobmonth    $fields['month'];
                
    $dobyear    $fields['year'];
                
    $dob        $dobday."-".$dobmonth."-".$dobyear;

                
    $this->register($username$password$email$forename$surname$title$dob);
                
    header('Location: register.php?success');
                exit();
            } 
        } 
    That's the function. The print_r within the function displays its contents ok but any attempt to access the $errors variable from the parent page results in an error. It's either losing its value along the way or isn't passing correctly to begin with

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Its a scope problem.
    $errors isn't a class member, and php doesn't use variable masking like some other OO languages. You do have several options though.
    1. Create a member property for errors, than simply assign with $this->errors. Then read them from a getErrors() method. Problem is you also need to manage the disposal of it.
    2. Return $errors from the validate_registration.
    3. Accept a by-reference array argument to the function and place errors within it (would require modification for both call and signature, not particularly recommended unless you have planned around it)
    4. Use an independent class for logging

    For ease of use with the fewest modifications required, I would use either #2 or #4.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    grahamd1984 (01-03-2014)

  • #5
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post
    I appreciate the help! I've actually been trying your second solution but dropping in a
    PHP Code:
    return $array 
    at the end of the validation function doesn't seem to work - this is the stage at which I seem to lose the data in transit. Any ideas why it's not returning correctly?

  • #6
    Regular Coder
    Join Date
    Aug 2006
    Location
    Richmond, CA
    Posts
    185
    Thanks
    3
    Thanked 10 Times in 9 Posts
    Quote Originally Posted by grahamd1984 View Post
    I appreciate the help! I've actually been trying your second solution but dropping in a
    PHP Code:
    return $array 
    at the end of the validation function doesn't seem to work - this is the stage at which I seem to lose the data in transit. Any ideas why it's not returning correctly?
    Of course it should be
    PHP Code:
    return $errors
    if you want to access it as a return value.

    I'm a bigger fan of the logging though.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That's right. You'll probably want to rename the function as well; if it returns anything other than false (well, void but that's comparable to null which in turn compares to false), it will indicate an error condition which IMO doesn't match the function name.
    You would then use it as such:
    PHP Code:
    if ($result $obj->validate_registration($json))
    {
        
    printf("An error has occurred: %s" PHP_EOLimplode("<br/>"$result));

    for example. That's why the name looks so unusual as well. Not that it really matters of course.
    If you instead use an error array passed in, that would look a little cleaner:
    PHP Code:
    $obj->validate_registration($json$errors);
    if (!empty(
    $errors))
    {
        
    printf("An error has occurred: %s" PHP_EOLimplode("<br/>"$result));

    The only reason I don't put something like that at the top is because I don't know if there are more than one places where this has to be called.

    The logging is IMO the best approach, but requires an additional class implementation.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

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