0

I have a php function:

function myFunc(MyClass inst) {
    // ...
}

Sometimes, when I call this function, I dont want to pass any arguments, but this doesn't work:

myFunc(null);

The error message is:

... must be an instance of MyClass , null given
Iter Ator
  • 8,226
  • 20
  • 73
  • 164
  • possible duplicate of [How do you create optional arguments in php?](http://stackoverflow.com/questions/34868/how-do-you-create-optional-arguments-in-php) – Rishi Dua Nov 16 '14 at 14:37

2 Answers2

5

Make your function's arguments optional, by providing default values. So instead of

function myFunc(MyClass inst)

it should be

function myFunc(MyClass inst=null)

See docs http://php.net/manual/en/functions.arguments.php

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
1

Just put the default initializer like so:

function myFunc(MyClass inst=null) {
    // ...
}

Then if you don't want to pass params, call it without ones :D

Pri1me
  • 117
  • 2
  • 9