错误:在null

时间:2018-01-09 19:57:17

标签: php

嘿,我一直在测试我的代码时遇到同样的错误。我使用的是PHP 7.2。一些语法适用于7.2,因此它可能看起来不同。我一直收到以下错误。

  

错误:在null

上调用成员函数cipher()
<?php

namespace Genial\Cryptography\Password;

use \Genial\Cryptography\
{
    Utils,
    Exception\RangeException,
    Exception\LengthException
};

/**
 * Bcrypt.
 */
class Bcrypt extends AbstractPasswordHash
{

    /**
     * @var int $cost The cost `Bcrypt` should use during execution.
     */
    private $cost = 10;

    /**
     * __construct().
     *
     * Set the avaliable options for `Bcrypt` hashing.
     *
     * The salt option has been deprecated as of PHP 7.0.0.
     *
     * @param int $cost The cost `Bcrypt` should use during execution.
     *
     * @return void.
     */
    function __construct(int $cost)
    {
        if ($cost < 2)
        {
            throw new RangeException(\sprintf(
                '`%s` The cost passed is too low. Passed: `%s`.',
                __METHOD__,
                \htmlspecialchars($cost, \ENT_QUOTES, 'UTF-8')
            ));
        }
        $this->cost = $cost;
    }

    /**
     * getCost().
     *
     * Get the current cost set.
     *
     * @return int Returns the current cost set.
     */
    public function getCost()
    {
        return $this->cost;
    }

    /**
     * cipher().
     *
     * Hash the plaintext using `Bcrypt`.
     *
     * @param string $plaintext The plaintext to hash during execution.
     *
     * @return string Returns the plaintext hashed using `Bcrypt`.
     */
    public function cipher(string $plaintext): string
    {
        if (\mb_strlen($plaintext) > 72)
        {
            throw new LengthException(\sprintf(
                '`%s` The password is longer than 72 characters. Password length: `%s`.',
                __METHOD__,
                \htmlspecialchars(\mb_strlen($plaintext), \ENT_QUOTES, 'UTF-8')
            ));
        }
        return (string) \password_hash($plaintext, \PASSWORD_BCRYPT,
        [
            'cost' => $this->cost,
        ]);
    }

    /**
     * verify().
     *
     * @param string $plaintext The plaintext to verify against hash and rehash if needed.
     * @param string $hash      The hash the plaintext should mtch up to.
     *
     * @return array Returns an array wich contains either one or two keys, one is to tell you if the password is correct
     *               and the other one is a new hash that the old one should be replaced with.
     */
    public function verify(string $plaintext, string $hash): array
    {
        if (\mb_strlen($plaintext) > 72)
        {
            throw new LengthException(\sprintf(
                '`%s` The password is longer than 72 characters. Password length: `%s`.',
                __METHOD__,
                \htmlspecialchars(\mb_strlen($plaintext), \ENT_QUOTES, 'UTF-8')
            ));
        }
        if (\password_verify($plaintext, $hash))
        {
            $temp = $hash;
            if (\password_needs_rehash($hash, \PASSWORD_BCRYPT,
                [
                    'cost' => $this->cost,
                ])) {
                $hash = $this->cipher($plaintext);
            }
            if (!Utils::hashEquals($temp, $hash))
            {
                return (array) [
                    'password_verified' => \true,
                    'new_hash' => $hash
                ];
            }
            return (array) [
                'password_verified' => \true
            ];
        }
        return (array) [
            'password_verified' => \false
        ];
    }

}

Utils::hashEquals相当于hash_equals()。 见http://php.net/manual/en/function.hash-equals.php

当我将类构造成变量并且我$bcryptClass->cipher('Hello world!')时,它会在下面显示此错误。

  

错误:在null

上调用成员函数cipher()

例外中没有方法,所以不是它们。

这是抽象类。

<?php

namespace Genial\Cryptography\Password;

/**
 * AbstractPasswordHash.
 */
abstract class AbstractPasswordHash
{

    /**
     * getHashInfo().
     *
     * @param string $hash The hash that will be tested.
     *
     * @return array Returns an array containing the hashing information used.
     *               It will not return the actual plaintext.
     */
    public function getHashInfo(string $hash): array
    {
        return (array) \password_get_info($hash);
    }

}

这是我在下面测试过的。

<?php

use Genial\Cryptography\Password\Bcrypt;

$bcrypt = new Bcrypt(12);

// There has been no error yet.

echo $bcrypt->cipher('Hello world!');

// Outputs error
// Error: Call to a member function cipher() on null

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

看起来在

中没有生成类Bcrypt的实例
$bcrypt = new Bcrypt(12)

因为错误本身表示不能在NULL上调用成员函数。

因此,请尝试验证是否已创建新实例。