简体   繁体   English

PHP错误无法在写上下文中使用方法return> value

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

I am getting this error in a PHP class... 我在PHP类中收到此错误...

Fatal error: Can't use method return value in write context in C:\\webserver\\htdocs\\friendproject2\\includes\\classes\\User.class.php on line 35 致命错误:无法在第35行的C:\\ webserver \\ htdocs \\ friendproject2 \\ includes \\ classes \\ User.class.php中的写入上下文中使用方法返回值

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. 这个代码在我的控制器中,是一个值尚未设置为$ this-> session-> get('user_id')然后它将返回false而不是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. 您不能将isset用于函数的结果。 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. isset()仅适用于变量,因为传递任何其他内容都会导致解析错误。 For checking if constants are set use the defined() function. 要检查是否设置了常量,请使用defined()函数。

From the PHP Manual . PHP手册

You can't use isset on a function. 您不能在函数上使用isset However, since false , 0 , and '' all equate to a falsey statement, write your test this way: 但是,因为false0''都等同于falsey语句,所以以这种方式编写测试:

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. 这样您就可以运行测试一步分配变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM