Norman
Norman

Reputation: 6365

How to avoid too many arguments when calling functions in php

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?

Upvotes: 1

Views: 1870

Answers (5)

Aldee
Aldee

Reputation: 4538

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.

Upvotes: 0

Orangepill
Orangepill

Reputation: 24665

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");

Upvotes: 4

FireCrakcer37
FireCrakcer37

Reputation: 1026

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

Upvotes: 2

BPm
BPm

Reputation: 2994

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' => '',...)

Upvotes: 0

Frederico Schardong
Frederico Schardong

Reputation: 2115

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
    }
}

Upvotes: 0

Related Questions