简体   繁体   English

`return $ this-> foo()=== null'是什么意思?

[英]What does `return $this->foo() === null` mean?

this is a code sample: 这是一个代码示例:

class md {
public function __construct() {
    if($this->getIsGuest()){
        echo 'iam guest';
    }
}
public function getIsGuest() {
    return $this->getState('__id')===null;
}
public function getState($val) {
    return '3';
}


is return $this->foo() === null is the default value of foo of nothing returned ? 返回return $ this-> foo()=== null是foo的默认值是什么也不返回?

getIsGuest() is returning the comparison of $this->getState('__id') to null. getIsGuest()将$ this-> getState('__ id')的比较返回为null。

If $this->getState('__id') returns exactly null , then getIsGuest() function returns true. 如果$this->getState('__id')返回正好为null ,则getIsGuest()函数返回true。 If it returns anything except exactly null , then the getIsGuest() function returns false. 如果返回除完全为null之外的任何内容,则getIsGuest()函数将返回false。

=== is type equality in php. ===是php中的类型相等。 In this case it would be used to tell the differencee between 0, '', false or the actual value null. 在这种情况下,将使用它来区分0,'',false或实际值null之间的差异。

This isn't all too good a sample, but there's nothing default-value-ish to it. 这并不是一个很好的示例,但是它没有默认值。 What you can be sure of, is that the getIsGuest method will always return a boolean (either true or false ). 可以肯定的是什么,就是getIsGuest方法将总是返回一个布尔值(或truefalse )。 In your code snippet, it'll always return false, because it uses the return value of another method ( getState ) and compares it to null strictly (value and type). 在您的代码段中,它将始终返回false,因为它使用另一个方法的返回值( getState )并将其严格地与null (值类型)进行比较。 Since the getState member function is hard-coded to return a char in your example, it will never return null, and therefore, getIsGuest will always return false. 由于在您的示例中getState成员函数经过硬编码以返回char,因此它绝不会返回null,因此, getIsGuest始终返回false。

PHP has null as its default return value for all functions or methods that have no explicit return statement (with the possible exception of a constructor, which returns an instance of an object). 对于没有显式return语句的所有函数或方法(对于可能返回构造函数实例的构造函数除外),PHP将null作为其默认返回值。 Just like JS will return undefined by default (except constructors, again), or C functions can return void , but that's another matter. 就像JS在默认情况下会返回undefined (再次是构造函数除外)一样,或者C函数可以返回void ,但这是另一回事。
In short: no, your code isn't setting a default return value. 简而言之:不,您的代码未设置默认返回值。 I'll attempt to clarify all this by editing your snippet a bit: 我将尝试通过稍微修改一下代码片段来澄清所有这些问题:

class md
{
    private $_mdData = array();//no data
    public function __construct(array $params = null)//default value of $params is null
    {//the constructor expects either no parameters, or an array
        if ($params !== null)
        {
            $this->_mdData = $params;
        }
        if($this->getIsGuest())
        {
            echo 'I\'m guest';
        }
    }

    public function getIsGuest()
    {
        return $this->getState('__id') === null;
    }

    public function getState($val)
    {//return whatever value is stored in the private array under $val-key
     //if the key doesn't exist, null is returned
        return (isset($this->_mdData[$val]) ? $this->_mdData[$val] : null);
    }
}

$guest = new md();//no params

constructor calls getIsGuest, which tries to access $ mdData[' _id'] 构造函数调用getIsGuest,它尝试访问$ mdData [' _id']
$_mdData is empty, so the key doesn't exist and null is returned to getIsGuest. $ _mdData为空,因此键不存在,并且null返回给getIsGuest。
getIsGuest compares the return value (null) to null, and returns true(because they are the same) getIsGuest将返回值(null)与null进行比较,并返回true(因为它们相同)
The constructor receives true, because that's where the getIsGuest method was called, and evaluates the value if ($this->getIsGuest()) --> true, so I'm a guest is echoed. 构造函数接收到true,因为在那是调用getIsGuest方法的地方,并评估if ($this->getIsGuest()) -> true的值,所以回显了I'm a guest

$nonGuest = new md(array('__id'=>123));//same drill, only this time getState will return 123, and getIsGuest returns false

Now the difference between == and === : 现在=====之间的区别:

$nullish = new md(array('__id' => ''));

This won't echo I'm a guest, Unless you were to change return $this->getState('__id') === null; 不会回应我是客人, 除非您要更改return $this->getState('__id') === null; to return $this->getState('__id') == null; return $this->getState('__id') == null; because '' an empty string is null-ish, like 0 is falsy etc... 因为''一个空字符串为空,例如0代表虚假等...

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

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