致命错误:未捕获的错误:在中找不到类“用户名”

I have the following code, where I want to instantiate the class Username by $object = new Username. The class Username extends to class Database (which is the class in the require_once() command). And I get the error Fatal error: Uncaught Error: Class 'Username' not found in..... But when I change class Username extends Database to class Username (i.e., When it doesn't extend anymore to class Database), I get the error Fatal error: Uncaught Error: Call to undefined method Username::some() in..... I don't know what's my mistake..

<?php
require_once 'database.class.php';

if(isset($_POST['some'])){
    $sql = 'SELECT * FROM noti WHERE name=:name';
    $params = [
        'name' => $_POST['some']
    ];
    $object = new Username;
    $number = $object->some($sql, $params);
    if($number>0){
        echo 'Username Exists.';
    }
    else{
        echo '';
    }
}

class Username extends Database{
    public function some($sql, $params){
        return $this->num_rows($sql, $params);
    }
}
评论
白风
白风

It's because your logic which attempts to instantiate Username is above the code which defines it. So, when you attempt to instantiate it, it is not defined - yet. Put the class definition before its instantiation.

点赞
评论