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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Location
    Philadelphia
    Posts
    241
    Thanks
    2
    Thanked 5 Times in 5 Posts

    Opptional Arguments Defining Functions

    Now Im turning into a guy who makes a lot of functions and classes and I've stumbled upon errors when leaving so of my own function arg's blank....now I know all the predefined functions make arg's opptional, so how can I make my function arg's opptional and I'm making them in classes so inc. that...


    Thanks In Advance

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    note that you can't have optional args passed by reference so you can't do &$optional_arg='' etc otherwise ...
    PHP Code:
    <?
    function blah$arg1 $optional_arg='' $optional_arg2='' ){
      if( !empty( 
    $optional_arg ) ){
         
    //do_stuff//
      
    }
    }
    blah'yaks' ) ;
    blah'yaks' 'yaks' ) ;
    blah'yaks' '' 'yaks' ) ;
    ?>
    this can get confusing sometimes so I like to use null when not passing an arg (so all calls at least look the same)
    blah( 'yaks' , null , 'yaks') ;
    but then you should be checking for is_null() within your function

    And some will say the above is bad juju , perhaps thats true , I am now in the habit of passing a optional array of data instead , this makes things far easier to refactor if you need to another argument to a common function at some future point and do not want to trawl your code changing function calls to do so

    PHP Code:
    <?
    $optional_args 
    = array('arg2'=>'blah','arg3'=>'blah') ;
    blah('yaks' $optional_args ) ;
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Location
    Los Angeles
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Firepages second example is the same thing I use and it really works great. It makes adding args to class methods very easy plus you can do things like this:

    PHP Code:

    class objSQL() {

      function 
    edit($table,$values) {   //$values could equal $_POST or some other Global array
        
    $sql  "UPDATE $table ";
        
    $sql .= " SET";
        foreach(
    $values as $key=>$value) {
          ${
    $key} = htmlentities($value);
          
    $sql .= "    $key='" . ${$key} . "',";
        }
        
    $sql substr$sql,0,(strlen($sql)-1) ); //Remove the last comma
        
    $sql .= "    WHERE {$this->unique}='"$this->uniquid "'";  //Primary Key

        
    return $this->query($sql);
      }




  •  

    Posting Permissions

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