1

Is there a way to avoid too many arguments when calling a function?

Eg:
function myFunction($usrName,$usrCountry,$usrDOB//and many more){
    // Do something with all those arguments here
}

One way I do this is to define constants

//After thorough checking

$sesUserName = $_SESSION['sesUserName'];

define('USERNAME', $sesUserName);

myFunction(){
    // Do something with USERNAME here
    // No need to use USERNAME when calling the function
}

But are there other ways to do this?

3
  • I wouldn't do what you're doing with constants there. If you really want to access $sesUserName from within myFunction(), you can call global $sesUserName; as the first line in myFunction(). This is a step up from the constant thing, but it's still generally discouraged. You can also access superglobals (the $_ arrays like $_SESSION) without doing the global thing, so you can access $_SESSION['sesUserName'] from within your function directly. This is probably not the best approach either, though. I'd recommend using an associative array, as others have pointed out. Commented Jun 2, 2013 at 4:35
  • They are required. But like I said, I don't know a different way to do this. Commented Jun 2, 2013 at 4:38
  • you could parse an array Commented Jun 2, 2013 at 5:09

5 Answers 5

4

One approach would be to start using OOP programs and create a user Object. This will allow you to use a user as a single entity as opposed to a arbitrary group of properties or constants.

<?php 

   class User {
        public $name;
        public $country;
        public $dateOfBirth;
        // for stuff that a user does or is define as instance methods
        public function isUserOver18(){
            return time() < strtotime("+18 years", strtotime($dateOfBirth));
        }
   }

   $user = new User();
   $user->name = $data["name"];
   $user->country = $data["country"];
   $user->dateOfBirth = $data["dob"];

   if ($user->isUserOver18()){
       // show page
   } else {
       echo "You must be 18 years or older to view this video";
   }

   // for stuff that is done to a user pass it in as an argument. 

   notifyUser($user, "You've got mail");
Sign up to request clarification or add additional context in comments.

Comments

2

Instead of creating huge functions that require you to pass in a ton of params. Learn about Object Oriented Programming, and create a PHP class.

I promise, that once you learn these techniques you will look at programming completely different. You can create reusable classes for things that you do frequently such as database operations, user management systems, and much much more.

Mastering using objects and classes is what separates mediocre programmers from great programmers.

Here is a good beginner tutorial on object oriented programming in PHP

Comments

0

You can pass an array of arguments:

function myFunction($arrayArgs){
    if(is_array(arrayArgs){
        if(!array_key_exists('usrName', $arrayArgs)){
            return null;
        }

        if(array_key_exists('usrCountry', $arrayArgs)){
            return null;
        }

        //and many other ifs
    }
}

Comments

0

are those params all required? You could just have 1 array as a param with all the required keys.

E.g: function myFunc($arr) and the array would be array('user_name' => '', 'user_country' => '',...)

Comments

0

Zend Framework 1.x actually had the very nice implementation for handling parameters. The rule is pretty simple. If you need 3 or less parameter, just directly specify it. However when you already need more than 3, you need the 3rd parameter to be an array where upon call, you just specify an array of key value pair values. Pretty nice actually based on experience.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.