Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same named function with multiple arguments in PHP

Tags:

function

php

I started off OOP with Java, and now I'm getting pretty heavy into PHP. Is it possible to create multiples of a function with different arguments like in Java? Or will the interpreted / untyped nature of the language prevent this and cause conflicts?

like image 444
Josh K Avatar asked Jan 27 '10 14:01

Josh K


People also ask

Can we have two functions with the same name in PHP?

Function overloading or method overloading is the ability to create multiple functions of the same name with different implementations. In simple words, “Overloading means declaring a function multiple times with a different set of parameters”.

Can you have two functions with the same name in a PHP class Why or why not?

Here is an explanation in more high level terms: Java supports Method overloading which is what you are referring to when you talk about function with the same name but different arguments. Since PHP is a dynamically typed language, this is not possible.

How many arguments can a PHP function have?

PHP native functions According to the manual, PHP functions may accept up to 12 arguments.

Can a method accept more than one argument?

The varargs functionality allows you to pass any number of arguments to a method. The method must be set up with the type of data and a variable name to hold the elements. You can add more parameters to the method, but the varargs statement must be the last one.


1 Answers

Everyone else has answers with good code explanations. Here is an explanation in more high level terms: Java supports Method overloading which is what you are referring to when you talk about function with the same name but different arguments. Since PHP is a dynamically typed language, this is not possible. Instead PHP supports Default arguments which you can use to get much the same effect.

like image 113
Poindexter Avatar answered Sep 22 '22 14:09

Poindexter