简体   繁体   English

致命错误:未捕获错误 OOP php 扩展 class

[英]Fatal error: Uncaught Error OOP php extends class

I did mvc app.我做了 mvc 应用程序。

There is a class database in file db.php.在文件 db.php 中有一个 class 数据库。

<?php
namespace App\DB_con;

class DataBase{

    static $user = "root";
    static $pass = '1234';
    static $host = "localhost";
    static $db   = "mvcApp";

     static function conn_To_DB()
    {

        $user = self::$user;
        $pass = self::$pass;
        $host = self::$host;
        $db   = self::$db;

        $conn = new PDO("mysql:dbname=$db;host=$host", $user, $pass);
        return $conn;

    }
}

I inherit it in another file model_tasks.php.我在另一个文件 model_tasks.php 中继承它。

<?php

namespace app\contol\model;
require_once 'conf/db.php';


use App\DB_con\DataBase as main_model;



class model_tasks extends main_model
{
        public function add_tasks($name, $email){
            $db  = DataBase::conn_To_DB();
            $sql = "INSERT INTO  tasks (name, mail) VALUES ('$name','$email')";
            $result = $db->prepare($sql);
            return $result->execute();

        }
}

Data that get into the method var_dump well.进入方法 var_dump 的数据很好。 Why the inherited class does not find the class from which it receives the connection to the database error appears?为什么继承的 class 找不到从其接收到数据库连接的 class 错误? What am I doing wrong?我究竟做错了什么?

  Fatal error: Uncaught Error: Class 'app\contol\model\DataBase' not found in /models/model_tasks.php:14 
Stack trace: #0 /controllers/HomeController.php(28): app\contol\model\model_tasks::add_tasks('', '', '') 
#1 /conf/route.php(28): App\Controller\BasicController::add_post() 
#2 /index.php(3): require_once('/....') 
#3 {main} thrown in /models/model_tasks.php on line 14

You use namespace App\DB_con\DataBase via alias main_model , that's why your script doesn't know about original namespace App\DB_con\DataBase , it knows about alias main_model .您通过别名main_model使用命名空间App\DB_con\DataBase ,这就是为什么您的脚本不知道原始命名空间App\DB_con\DataBase ,它知道别名main_model

But going further as your model_tasks extends your main_model , all parent class methods are available in a child class.但是,随着您的model_tasks扩展您的main_model ,所有父 class 方法都在子 class 中可用。

So,所以,

$db = DataBase::conn_To_DB();

can be changed to:可以改为:

$db = static::conn_To_DB();  // or less secure $db = self::conn_To_DB();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP致命错误:未捕获错误:找不到类 - PHP Fatal error: Uncaught Error: Class not found Phpseclib PHP 致命错误:未捕获错误:Class - Phpseclib PHP Fatal error: Uncaught Error: Class 一个 class 得到“PHP 致命错误:未捕获的错误:类...”,但其他没有 - One class getting “PHP Fatal error: Uncaught Error: Class…” But not the others PHP致命错误:未捕获错误:找不到类'Facebook \ WebDriver \ ChromeOptions' - PHP Fatal error: Uncaught Error: Class 'Facebook\WebDriver\ChromeOptions' not found PHP致命错误:未捕获错误:未在中找到类“ ZipArchive” - PHP Fatal error: Uncaught Error: Class 'ZipArchive' not found in PHP的致命错误:未捕获的错误:类“路由器”中找不到 - php Fatal error: Uncaught Error: Class 'Router' not found in 严重错误:未捕获的错误:未找到类“缓存”-PHP - Fatal error: Uncaught Error: Class 'cache' not found - PHP PHP 致命错误:未捕获错误:Class 未找到异常 - PHP Fatal error: Uncaught Error: Class Exception not found [PHP] [Zend]致命错误:未捕获的错误:类&#39;Film \\ Model \\ Film&#39; - [PHP][Zend] Fatal error: Uncaught Error: Class 'Film\Model\Film' PHP致命错误:未捕获错误:未找到类“ Api” - PHP Fatal error: Uncaught Error: Class 'Api' not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM