开发者

Empty() function might not be working with Constants in PHP

I've been looking at it for a while and cannot figure out what the problem might be:

My Code:

<?php

    session_start();

    require "includes/config.php";
    require "includes/database.php";
    require "开发者_如何学Ctwitteroauth/twitteroauth.php";

    if(empty(CONSUMER_KEY) && empty(CONSUMER_SECRET))
    {
        exit("Please define Consumer Key and Consumer Secret Keys");
    }

    $connection = new TwitterOAuth(CONSUMER_KEY,CONSUMER_SECRET);
    $request_token = $connection->getRequestToken(OAUTH_CALLBACK);

    // Temporary credentials to make requests to Twitter
    $_SESSION["oauth_token"] = $request_token["oauth_token"];
    $_SESSION["oauth_token_secret"] = $request_token["oauth_token_secret"];

    switch ($connection->http_code)
    {
        case 200:
            $url = $connection->getAuthorizeURL($request_token);
            header("Location:".$url);
            break;
        default:
            die("Connection to Twitter failed. Please try again."); 
    }

?>

And the error is:

 ## 
Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in /home/.../connect.php on line 9
 ##

The line 9 part starts from

if(empty(CONSUMER_KEY) && empty(CONSUMER_SECRET))
{
    exit("Please define Consumer Key and Consumer Secret Keys");
}

CONSUMER_KEY and CONSUMER_SECRET, they are constants and defined in "includes/config.php" like below:

$config = parse_ini_file("config.ini");
define("CONSUMER_KEY",$config["TwitterAPI"]["Consumer_Key"]);
define("CONSUMER_SECRET",$config["TwitterAPI"]["Consumer_Secret"]);


Use defined instead of empty to check if a constant exists. empty and isset functions check if variables exist. If you're wanting to check if it's empty then just check the length.

Very random that this exact error happened to me about 2 hours ago...


empty() and isset() are both language constructs. They are not functions. The basic difference is that with a normal function, the value is passed as a parameter to the function. So it doesn't matter what you put in the call, it's the final evaluated response that's sent to the function. But with a language construct, it operates on what's inside the braces directly. There's no "passing", as there's nothing to pass to.

So, empty() and isset() are made for inspecting variables only. Otherwise it can't really inspect the variable since there is no variable to inspect. A limitation, sure. But a fairly easy one to workaround.

Now, in your case, you're trying to do if (empty(CONSTANT)). That won't work. But let's look at how we could do that:

empty() is basically a wrapper for isset($foo) && $foo. Now, isset() is a wrapper for !is_defined($foo) && !is_null($foo) (where is_defined is a function I just made up). So if we wanted empty like behavior with non-variables, all we'd need to do is:

if (CONSTANT) {

HOWEVER

I'd strongly revisit what you're trying to do. With the exception of debugging switches, you should not be using constants in a way where they could have more than one value ever. If they can have more than one value (configuration setting, etc), then they are not constants but are global variables disguised as constants. Personally, I'd refactor the use of those constants out to configuration variables that are injected or managed better than globals.

So what do I use constants for? For things that are truely constant. define('DOZEN', 12), define('BITS_PER_BYTE', 8), define('DAYS_PER_WEEK', 7). But I try to avoid at all costs constants which may be adjusted later on. For those uses, I would use a variable instead, as it's more semantically correct and is easier to work with for that context.


maybe if(!isset(CONSUMER_KEY) && !isset(CONSUMER_SECRET))? "PAAMAYIM NEKUDOTAYIM" in hebrew means double colon (T_PAAMAYIM_NEKUDOTAYIM).


empty() is a tricky function. Basically empty can be used only for variables. You should instead use

if(defined('CONSUMER_KEY') && (constant('CONSUMER_KEY'))){

}


It looks like you'll need to cast the constant to a boolean:

if (((boolean) CONSUMER_KEY) && ((boolean) CONSUMER_SECRET))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜