Is declaring a variable inside a bad practice method call?

If I were to call a method that took a parameter and then defined a variable at the same time, would that be considered "bad practice"?

Example:

if( file_exists( $file = "skins/Default/Controllers/Demo.php" ) )
{
    require( $file );
}

It seems to me that this simplifies the process, since it does not require the creation of another variable above, and does not clutter up the code by writing a line twice.

+4
source share
2 answers

Declares a variable inside a call to a bad practice method?

Yes, because it hides the intent of other functions.

$file = "skins/Default/Controllers/Demo.php";
if (file_exists($file)) {
    require($file);
}

easier to read and reason than:

if (file_exists($file = "skins/Default/Controllers/Demo.php")) {
    require($file);
}

because it would be easily mistaken for $file == "skins/Default/Controllers/Demo.php"what is usually found in the instructions if.

+7
source

, (= > ) .

$file = "skins/Default/Controllers/Demo.php";
if( file_exists( $file ) )
{
    require( $file );
}

0

All Articles