PHP7 - 未捕获错误:访问未声明的静态属性

时间:2016-05-31 21:11:51

标签: php

我一直试图玩这里发现的这个骰子计算器: https://github.com/ringmaster/dicecalc

以前所有功能都按预期运行,我可以毫不费力地“滚动”3d6,3d6 + 2,1d12等。

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

  

mod_fcgid:stderr:PHP致命错误:未捕获错误:访问未声明的静态属性:/public_html/dice/src/DiceCalc/CalcOperation.php:34中的DiceCalc \ CalcOperation :: $运算符

我已经对访问未声明的静态属性进行了一些搜索,但无济于事。我试过在每个上面都返回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);

我刚刚开始围绕课程等等,所以我不知道如何解决这个问题。我已经尝试切换返回self ::返回static ::在我通过搜索这里找到的其他一些东西。我现在才被困住了。有人能帮忙吗?

2 个答案:

答案 0 :(得分:8)

这是由change in the way PHP 7.0+ parses code

引起的
  

对变量,属性和方法的间接访问现在将严格按照从左到右的顺序进行评估,而不是之前的特殊情况组合。下表显示了评估顺序的变化情况。

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]被混合为“$operator数组的self::$operators键”(不存在)。

答案 1 :(得分:1)

$operators定义为类的静态属性:

class CalcOperation {

    static $operators = array(
            '+' => 'add',
            '*' => 'multiply',
            '=' => 'equalto',
            '<' => 'lessthan',
            '>' => 'greaterthan',
            '^' => 'exponent',
            '/' => 'divide',
            '-' => 'subtract'
        );

 /**
     * @param  string $operator
     * @param $operand2
     * @param $operand1
     *
     * @throws \Exception
     * @return bool|number
     */
    public function calc($operator, $operand1, $operand2) {
...
}
...//rest of class...

然后使用$operators

访问整个班级的self::$operators