Possible Duplicate:
Why 'defined() || define()' syntax in defining a constant
This piece of code is created by the zf tool that Zend Framework provides.
defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
My questions is : What is the purpose of this line of code? There are no conditional statements such as if, switch. Does it imply a conditional statement automatically?
Here is how i understand it:
if APPLICATION_PATH 开发者_JAVA技巧is defined, leave it alone else set it to : realpath(dirname(__FILE__) . '/../application')
.
If my assumption is true this is a really confusing syntax.
Any help will be appreciated.
Your assumption is correct. This is just a short hand way of saying
if (!defined('APPLICATION_PATH')) {
define('APPLICATION_PATH', '...');
}
You can easily test this:
define("foo", "bar");
defined("foo") || define("foo", "baz");
var_dump(foo);
Output is bar
.
//define("foo", "bar");
defined("foo") || define("foo", "baz");
var_dump(foo);
Output is baz
.
This code uses short circuiting to evaluate the expression -- only execute the stuff after the or operator (the ||
) if the first parameter fails to be true. Short circuiting works under the assumption that since only one value in the expression needs to be true in or for the whole thing to be true, execution can stop if the first argument is true -- preventing the second argument to the operator from being executed.
So the code
defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(_FILE_) . '/../application'));
Will check to see if APPLICATION_PATH
is defined. If it is, then the ||
must be true and there's no reason to check if the second argument is true or false as it won't impact the final outcome of the boolean operation.
If APPLICATION_PATH
is not defined, then the ||
must evaluate the second argument, (in this case defining APPLICATION_PATH
) in order to determine the outcome of the ||
operation.
So it works out to be effectively the same as
if (!defined('APPLICATION_PATH'))
{
define('APPLICATION_PATH', realpath(dirname(_FILE_) . '/../application'))
}
You'll also often see short circuit evaluation with the &&
operator. One example of this is a common idiom used in many languages is to NULL check in the if statement by checking for NULL first in the &&
expression:
if ( something != NULL && something->SomeCondition())
{
//...
}
This works the opposite of ||
short circuit evaluation. For &&
evaluation stops if the first argument is false, because everything must be true for the &&
to pass. So in the above code, something->SomeCondition()
won't cause any kind of crash if something is NULL -- it won't get executed because something != NULL was false and execution of the expression terminated at that point.
Yes, this is a shortcut (known as short-circuit evaluation) for the longer:
if (!defined('APPLICATION_PATH')) {
define('APPLICATION_PATH', realpath(dirname(_FILE_) . '/../application'));
}
The reason this works is the way the ||
in the middle works.
||
is the OR operator, and it returns TRUE if either of the values besides it are TRUE. If the first value is TRUE, then it needn't check the second one, so it doesn't. So, if defined('APPLICATION_PATH')
is TRUE then define('APPLICATION_PATH', realpath(dirname(_FILE_) . '/../application'))
will never run.
In this case, the value returned by the statement is simply discarded (it isn't used in another statement or stored in a variable), but the way the operator works still makes this technique work.
I wouldn't particularly recommend using it in code, since it can make the code less readable. However, it's a fairly widespread shortcut, so most programmers will recognize it when they see it.
it is something like
if (!defined('APPLICATION_PATH')) define('APPLICATION_PATH', realpath(dirname(_FILE_) . '/../application'));
or, in human's language
defined('APPLICATION_PATH') or define('APPLICATION_PATH', realpath(dirname(_FILE_) . '/../application'));
It's a ternary/inline conditional statement. If the defined
function returns false, it will define
the APPLICATION_PATH.
Equals to
if ( !defined('APPLICATION_PATH') )
define('APPLICATION_PATH', realpath(dirname(_FILE_) . '/../application'));
Your assumption is right, and this code is a mess.
||
is PHP's OR
operator.
If APPLICATION_PATH
is undefined, this line will define it.
精彩评论