8

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?

Community
  • 1
  • 1
Michal
  • 1,955
  • 5
  • 33
  • 56
  • 2
    You might try `$bar = $bar;` – RiggsFolly Mar 06 '16 at 16:08
  • Where are you seeing this warning? – Ray Mar 06 '16 at 16:16
  • @RiggsFolly: thanks I'll try it. – Michal Mar 06 '16 at 16:35
  • 2
    @Ray: In the majority of PHP development tools, for instance Netbeans – Michal Mar 06 '16 at 16:37
  • 2
    @Michal Ah, in an IDE. Based on your comment, this is an IDE specific warning for Netbeans. I'm using Eclipse and it doesn't flag this as an error, warning or notice. However, if I do: `$foo=$foo;` Eclipse complains with a notice "Assignment has no Effect". – Ray Mar 06 '16 at 16:44
  • 1
    @Ray Yes, same problem with $bar = $bar – Michal Mar 06 '16 at 16:49
  • `if (($bar = $bar)) {}` TLDR: Double brackets = Ignore this type of warning. http://stackoverflow.com/a/8357990/4621324 – Axalix Mar 06 '16 at 18:18
  • 4
    I would caution against a statement like $bar = $bar, because a) its only purpose is to defeat an IDE warning (which is there for a reason) and b) it decreases the readability/maintainability of the code. I'm assuming OP could just remove the function parameter from this example. PHP shouldn't complain if someone sends an extra param. Alternatively, I would bet you can turn off specific warnings in your IDE. – hcd00045 Jul 10 '17 at 18:40

2 Answers2

-1

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
  ...
}
Jedi14
  • 1
  • 2
-3

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)