PHP function parameters

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

I've been working on a problem all afternoon, and I couldn't figure out what was going on. My query results were always return the first column of my tables and nothing more. I finally descided to look into the database class that was being used and found an interesting bit of code.

PHP Code: [ Select ]
/**
    * Not sure why this is here either.
    *
    * @param string $query
    * @param boolean $cache Use Memcache
    * @param int $ttl Memcached's expiration time in seconds
    */
    public function customQuery ( $query , $cache = false , $ttl = 300 )
    {
        return $this -> query ( $query , $cache , $ttl );
    }
///
 
 /**
    * Queries the database for a mysql result set link or the processed result set.
    *
    * @param string $query The query to execute
    * @param string $method The method to fetch the result set link with
    * @param boolean $multiple If there are multiple rows to be fetched
    * @param boolean $cache Use Memcache
    * @param int $ttl Memcached's expiration time in seconds
    *
    * @return mixed The mysql result set link or the processed result set, depending on weather or not $method was passed.
    */
    public function query ( $query , $method = false , $multiple = false , $cache = false , $ttl = 300 )
    {
         /// Trimmed
}
 
  1. /**
  2.     * Not sure why this is here either.
  3.     *
  4.     * @param string $query
  5.     * @param boolean $cache Use Memcache
  6.     * @param int $ttl Memcached's expiration time in seconds
  7.     */
  8.     public function customQuery ( $query , $cache = false , $ttl = 300 )
  9.     {
  10.         return $this -> query ( $query , $cache , $ttl );
  11.     }
  12. ///
  13.  
  14.  /**
  15.     * Queries the database for a mysql result set link or the processed result set.
  16.     *
  17.     * @param string $query The query to execute
  18.     * @param string $method The method to fetch the result set link with
  19.     * @param boolean $multiple If there are multiple rows to be fetched
  20.     * @param boolean $cache Use Memcache
  21.     * @param int $ttl Memcached's expiration time in seconds
  22.     *
  23.     * @return mixed The mysql result set link or the processed result set, depending on weather or not $method was passed.
  24.     */
  25.     public function query ( $query , $method = false , $multiple = false , $cache = false , $ttl = 300 )
  26.     {
  27.          /// Trimmed
  28. }
  29.  


As far as I am aware you can't do this in PHP. It doesn't care what name you give a variable, parameters are always assigned in order. Am I correct on this or have I been missing something all these years.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

That's the way I've always understood it.

PHP Code: [ Select ]
<?php
 
class test
{
        function second($one = 1, $three = 3, $five = 5)
        {
                $this->first($one, $three, $five);
        }
 
        function first($one = 1, $two = 2, $three = 3, $four = 4, $five = 5)
        {
                echo "\n" . print_r(func_get_args(), true) . "\n";
        }
}
 
$obj = new test();
$obj->second();
 
?>
  1. <?php
  2.  
  3. class test
  4. {
  5.         function second($one = 1, $three = 3, $five = 5)
  6.         {
  7.                 $this->first($one, $three, $five);
  8.         }
  9.  
  10.         function first($one = 1, $two = 2, $three = 3, $four = 4, $five = 5)
  11.         {
  12.                 echo "\n" . print_r(func_get_args(), true) . "\n";
  13.         }
  14. }
  15.  
  16. $obj = new test();
  17. $obj->second();
  18.  
  19. ?>


Code: [ Select ]
Array
(
  [0] => 1
  [1] => 3
  [2] => 5
)
  1. Array
  2. (
  3.   [0] => 1
  4.   [1] => 3
  5.   [2] => 5
  6. )
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

I'm not sure I understand what you're asking here :scratchhead:
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Keyword arguments like in python. You'll notice in the above code that the $ttl 300 is being assigned to $multiple.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

They are assigned by order, not name.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

It would be nice if you could namespace passed arguments, something like this.

Code: [ Select ]
function method($one = 1, $two = 2) {}

method(two:"alternate value");
  1. function method($one = 1, $two = 2) {}
  2. method(two:"alternate value");


Then $one would use the default value.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

Allow me to introduce you two.

Python, Jobert. Jobert, Python. I'll play wingman with PHP while you two get to know each other.

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 102 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.