PHP7 - 未捕获错误 : Access to undeclared static property

标签 php

我一直在尝试使用这里的这个骰子计算器: https://github.com/ringmaster/dicecalc

之前一切都按预期运行,我可以毫无问题地“滚动”3d6、3d6+2、1d12 等。

将我的 vps 升级到带有 php7 的 Ubuntu 16.04 后,我开始收到以下错误:

mod_fcgid: stderr: PHP Fatal error: Uncaught Error: Access to undeclared static property: DiceCalc\CalcOperation::$operators in /public_html/dice/src/DiceCalc/CalcOperation.php:34

我对访问未声明的静态属性 进行了一些搜索,但无济于事。我试过在他们每个人身上返回 static::。这是它引用的 CalcOperation.php:

<?php
/**
 * Class CalcOperation
 *
 * @package DiceCalc
 * @author  Owen Winkler <epithet@gmail.com>
 * @license MIT http://opensource.org/licenses/MIT
 */
namespace DiceCalc;
class CalcOperation {
    /**
     * @param  string $operator
     * @param $operand2
     * @param $operand1
     *
     * @throws \Exception
     * @return bool|number
     */
    public function calc($operator, $operand1, $operand2) {
        $operators = array(
            '+' => 'add',
            '*' => 'multiply',
            '=' => 'equalto',
            '<' => 'lessthan',
            '>' => 'greaterthan',
            '^' => 'exponent',
            '/' => 'divide',
            '-' => 'subtract'
        );
        if (isset($operators[$operator])) {
            return self::$operators[$operator](self::reduce($operand1), self::reduce($operand2));
        }
        throw new \Exception('Unknown operator "' . $operator . '".');
    }
    /**
     * @param $operand
     *
     * @return number|string
     * @throws \Exception
     */
    public function reduce($operand) {
        if (is_numeric($operand)) {
            return $operand;
        } elseif ($operand instanceof Calc) {
            return $operand();
        }
        throw new \Exception('This is not a number');
    }
    /**
     * @param  number      $operand1
     * @param  number      $operand2
     *
     * @return bool|number
     */
    public function add($operand1, $operand2) {
        return $operand1 + $operand2;
    }
    /**
     * @param  number      $operand1
     * @param  number      $operand2
     *
     * @return bool|number
     */
    public function multiply($operand1, $operand2) {
        return $operand1 * $operand2;
    }
    /**
     * @param  number      $operand1
     * @param  number      $operand2
     *
     * @return bool|number
     */
    public function subtract($operand1, $operand2) {
        return $operand1 - $operand2;
    }
    /**
     * @param  number    $operand1
     * @param  number    $operand2
     *
     * @return bool|number
     */
    public function divide($operand1, $operand2) {
        return $operand1 / $operand2;
    }
    /**
     * @param  number      $operand1
     * @param  number      $operand2
     *
     * @return bool|number
     */
    public function exponent($operand1, $operand2) {
        return pow($operand1, $operand2);
    }
    /**
     * @param  number $operand1
     * @param  number $operand2
     *
     * @return bool
     */
    public function greaterthan($operand1, $operand2) {
        return $operand1 > $operand2;
    }
    /**
     * @param  number $operand1
     * @param  number $operand2
     *
     * @return bool
     */
    public function lessthan($operand1, $operand2) {
        return ($operand1 < $operand2);
    }
    /**
     * @param  number $operand1
     * @param  number $operand2
     *
     * @return bool
     */
    public function equalto($operand1, $operand2) {
        return ($operand1 == $operand2);
    }
}

这是调用类的行:

$stack[] = CalcOperation::calc($step, $r2, $r1);

我刚刚开始全神贯注于类(class)等,所以我不确定如何解决这个问题。我已经尝试将 return self::切换为 return static::以及我通过在此处搜索发现的其他一些内容。我现在被困住了。有谁能帮忙吗?

最佳答案

这是由 change in the way PHP 7.0+ parses code 引起的:

Indirect access to variables, properties, and methods will now be evaluated strictly in left-to-right order, as opposed to the previous mix of special cases. The table below shows how the order of evaluation has changed.

enter image description here

只是改变:

return self::$operators[$operator](self::reduce($operand1), self::reduce($operand2));

收件人:

return self::{$operators[$operator]}(self::reduce($operand1), self::reduce($operand2));

在 PHP7 中,self::$operators[$operator] 被解释为“self::$operators$operator 键” > 数组”(不存在)。

关于PHP7 - 未捕获错误 : Access to undeclared static property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37555693/

相关文章:

javascript - 在 PHP 代码中使用 JavaScript 访问动态创建元素的值

javascript - 如何使用 $_SERVER 在 PHP 上获取 POST 原始数据

javascript - 如何使用 PHP 偏移 HTML 表格以从不同列的第二行单元格值中减去第一行的单元格值

php - 如何使用 PHP 单击 DOM 元素并将其作为变量传递?

php - 在 PHP 中更改数字的符号?

php - 如何使用 jQuery/Ajax 和 PHP 检索 HTML 和变量

php - 将时间戳拆分为日期和时间并将它们插入关联数组中

php - 如何保护数据库服务器?

php - 为什么 DateTime::sub 没有按预期工作?

php - 在 Yii 中使用多个模型创建下拉列表