Michal
Michal

Reputation: 2039

PHP: How do I best silence a warning about unused variables?

I have an application in which i have to declare (override) methods inherited from an interface. However, these methods have parameters that are not used in my implementation.

class MyImplementation implements ISomething {

  public function foo($bar) {
    // code that does nothing with $bar
    ...
  }

  // other methods
  ...
}

How can I mark $bar as unused in the source code, in C++ this is possible.

In other words, how can I do THIS in PHP?

Upvotes: 8

Views: 7520

Answers (2)

Jedi14
Jedi14

Reputation: 11

To silence the "IDE" and the php compiler voluntary without change error_reporting and turn off warning, I work around the problem in 2 ways:

  1. by evaluating the variable:
($variable); // unused

the comment indicating that this syntax is intentional

  1. create a function that do nothing:
function unused() {};

.
.
.

unused($variable);

Here the name of the function indicate your voluntary intention. The avantage with a function is that you can passed more than one argument.

unused($foo, $bar);

I your case:

class MyImplementation implements ISomething {

  public function foo($bar) {
    // code that does nothing with $bar
    ($bar); // unused
    ...
  }

  // other methods
  ...
}

Upvotes: 0

Dries Meeuws
Dries Meeuws

Reputation: 1

if i understand your question correct, you would like to hide the error notices of uninitialized variables in your php script? If that's the case you chould change your error_reporting in php.ini

example: error_reporting=E_ALL & ~E_NOTICE (Show all errors, except for notices)

Upvotes: -3

Related Questions