PHP error Can't use method return > value in write context
I am getting this error in a PHP class...
Fatal error: Can't use method return value in write context in C:\webserver\htdocs\friendproj开发者_如何转开发ect2\includes\classes\User.class.php on line 35
Here is the troubled part.
if(isset($this->session->get('user_id')) && $this->session->get('user_id') != ''){
//run code
}
This code is in my contrustor, is a value is not already set for $this->session->get('user_id') then it will return false instead of a Number. So as you can see I was hoping to check if this value is a number or not or not even set.
Any help with fixing appreciated.
You can't use isset for the result of a function. Consider the following code instead:
if( $this->session->get('user_id') ){
//run code
}
isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.
From the PHP Manual.
You can't use isset
on a function. However, since false
, 0
, and ''
all equate to a falsey statement, write your test this way:
if( $id = $this->sessions->get('user_id') ){
// Will only run if $id does not equal '', False, or 0
}
That way you have run your test and assigned the variable in one step.
精彩评论