【问题标题】:Fatal error: Uncaught Error: Class 'Calc' not found in C:\wamp64\www\OOPCalculator\includes\calc.inc.php on line 9致命错误:未捕获的错误:第 9 行的 C:\wamp64\www\OOPCalculator\includes\calc.inc.php 中找不到类 'Calc'
【发布时间】:2021-02-28 23:35:20
【问题描述】:

我在互联网上找到了这段代码,但它不起作用,我一直收到这个错误。致命错误:未捕获的错误:在第 9 行的 C:\wamp64\www\OOPCalculator\includes\calc.inc.php 中找不到类 'Calc'

我不知道为什么会这样,但如果你能帮助我,请

declare(strict_types = 1);
include 'includes/class-autoload.inc.php';

$oper = $_POST["oper"];
$num2 = $_POST["num2"];
$num1 = $_POST["num1"];

$Calc = new Calc($oper, (int)$num1, (int)$num2);

try {
    echo $Calc->calculation();
}
catch (TypeError $e) {
    echo "Error!: ". $e->getMessage();
}
?>



<?php

class Calc {
    public $operater;
    public num1;
    public num2;
    
    public function __construct(string $one, int $two, int $three) {
        $this->operater = $one;
        $this->num1 = $two;
        $this->num2 = $three;

    }
    
    public function calculator() {
        switch($this->operater) {
            case 'add':
            $result = $this->num1 + $this->num2;
            return $result;
            break;
            
            case 'sub':
            $result = $this->num1 - $this->num2;
            return $result;
            break;
            
            case 'div':
            $result = $this->num1 / $this->num2;
            return $result;
            break;
            
            case 'mul':
            $result = $this->num1 * $this->num2;
            return $result;
            break;
            
            default:
            echo "Error!";
            break;
        }
    }
}
?>

【问题讨论】:

  • 自动加载器没有加载类。可能有很多问题。检查您的项目结构,看看它是否符合自动加载器规则。
  • edit你的问题包含class-autoload.inc.php的代码

标签: php


【解决方案1】:

检查您的自动加载器,它应该是这样的

    <?php
        spl_autoload_register('myAutoLoader');
        function myAutoLoader($className){
        $url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        if(strpos($url, 'includes') !== false){
        $path = '../classes/';
        }
        else{
        $path = './classes/';
        }
        $extension = ".class.php";
        require_once $path . $className . $extension;
        }
        ?>

【讨论】:

    【解决方案2】:

    请正确检查您班级中的财产声明。你错过了 $ 符号。

    class Calc {
        public $operater;
        public $num1;
        public $num2;
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-06
      • 2018-05-11
      • 2020-11-09
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多