Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-08-2004, 02:26 AM   PM User | #1
As5a5sIn5
Regular Coder

 
Join Date: Apr 2004
Location: Philadelphia
Posts: 241
Thanks: 2
Thanked 5 Times in 5 Posts
As5a5sIn5 is an unknown quantity at this point
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
As5a5sIn5 is offline   Reply With Quote
Old 10-08-2004, 04:16 AM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,943
Thanks: 7
Thanked 82 Times in 81 Posts
firepages will become famous soon enough
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)
firepages is offline   Reply With Quote
Old 10-08-2004, 07:47 AM   PM User | #3
Hawkmoon
Regular Coder

 
Join Date: Apr 2004
Location: Los Angeles
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Hawkmoon is an unknown quantity at this point
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);
  }


Hawkmoon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:49 AM.


Advertisement
Log in to turn off these ads.