dousao2186 2016-05-31 21:11
浏览 152
已采纳

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

I've been attempting to play with this dicecalculator found here: https://github.com/ringmaster/dicecalc

Previously everything functioned as expected, I could "roll" 3d6, 3d6+2, 1d12, etc without any problems.

After upgrading my vps to Ubuntu 16.04 with php7 on it, I started to receive the following error:

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

I've done a few searches on Access to undeclared static property to no avail. I've tried returning static:: on each of them. Here's the CalcOperation.php it's referencing:

<?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);
    }
}

And here's the line that calls the class:

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

I'm just beginning to wrap my head around classes etc so I'm not sure how to fix this. I've tried switching the return self:: to return static:: among a few other things I've found by searching on here. I'm just stuck now. Anyone able to help?

  • 写回答

2条回答 默认 最新

  • doushui3216 2016-05-31 21:19
    关注

    To define $operators as a static property of the class :

    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...
    

    Then access the $operators throughout your class using self::$operators

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler