#1
  1. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115

    Static versus reference versus global


    Hi guys I was fiddling a bit with an error reporting array (storing). Ideally I want to have several functions that pass along what goes wrong to an accessible array (so outside the scope of the function). For instance "creditcard number is invalid" (or whatever).

    The code below shows 4 (simplistic) methods to accomplish this. Apart from the first one they all have something I like.

    Anyone might want to say which method they favour or dislike and why?
    PHP Code:
    <?php
    /* #1 | most inefficient way */
    $errors = array();

    function 
    bla1($a){
        
    $a[] = 'some error text';
        return 
    $a;
    }

    $errors bla1($errors);

    var_dump($errors);

    /* #2 | with a pass by reference */

    $errors = array();

    function 
    MyReference(&$a){
        
    $a[] = 'some error text';

    }

    MyReference($errors);

    var_dump($errors);

    /* #3 | with a global variable */

    $errors = array();
    function 
    MyGlobal(){
        global 
    $errors;
        
    $errors[] = 'terror text';
    }

    MyGlobal();

    var_dump($errors);



    /* #4 | with a static function */
    class Errors{
        private static 
    $errors = array();
        
        public static function 
    setErrors($a){
            
    self::$errors[]= $a;
        }
        public static function 
    getErrors(){
            return 
    self::$errors;
        }
    }


    function 
    MyStatic(){
        
    Errors::setErrors('error text');
    }

    MyStatic();

    var_dump(Errors::getErrors());

    ?>
    P.s. fairly new to oop so if something is odd, please tell me
    Last edited by aeternus; January 2nd, 2013 at 03:08 PM.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    Well, how far OOP do you want to go? I would have an object representing the form itself (doesn't have to be so specific as to be for that individual form) which acts like a data repository. You can get the form data from it, even as simply as wrapping functionality around $_POST, and push validation/error messages into it.

    In general if you're considering anything remotely OOP then global variables and static/global functions are the wrong direction.
  4. #3
  5. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by requinix
    Well, how far OOP do you want to go? I would have an object representing the form itself (doesn't have to be so specific as to be for that individual form) which acts like a data repository. You can get the form data from it, even as simply as wrapping functionality around $_POST, and push validation/error messages into it.

    In general if you're considering anything remotely OOP then global variables and static/global functions are the wrong direction.
    Ha Requinix, As always thanks for your reply! I guess your right. This isn't really object orientated and I am more or less abusing it with static functions. I should indeed create some form class. (lazy as always )
    The pass by reference I like, but it's a bit of a pain in the * to add all those parameters. Hopefully I can soon show off my Form class. (edit: trying to build it now

    Thanks!
    Last edited by aeternus; January 2nd, 2013 at 03:50 PM.

IMN logo majestic logo threadwatch logo seochat tools logo