开发者

PHP error Can't use method return > value in write context

开发者 https://www.devze.com 2022-12-16 20:27 出处:网络
I am getting this error in a PHP class... Fatal error: Can\'t use method return value in write context in

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.

0

精彩评论

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

关注公众号