简体   繁体   English

当包含或需要外部文件时,无法使用后期静态绑定在PHP中扩展类

[英]Cannot extend class in PHP using Late Static Binding when including or requiring external file

Here's the source class: 这是源类:

require_once('database.php');

class DatabaseObject {

    public static function find_all()
    {
        $sql = "SELECT * FROM " .static::$dbName;
        $result = mysql_query($sql);
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {
            echo "<div class = user>";
            echo "Name: {$row['first_name']} {$row['last_name']} <br/>";
            echo "E-Mail: {$row['email']} <br/>";
            echo "Username: {$row['username']} <br/>";
            echo "Password: {$row['password']}";
            echo "</div> <hr/>";
        }
    }


}

And here's the inheriting class: 这是继承的类:

require_once('database.php');
require('databaseobject.php');

class User extends DatabaseObject{

    public $id;
    public $first_name;
    public $last_name;
    public $emai;
    public $username;
    public $password;

    public static $dbValues = array(
        'id','first_name','last_name',
        'email','username','password'
    );
    public static $dbName = 'users';

}

User::find_all();

After trying to run user.php I get the following output: 尝试运行user.php后,我得到以下输出:

> "; echo "E-Mail: {$row['email']} 
"; echo "Username: {$row['username']} 
"; echo "Password: {$row['password']}"; echo "
"; } } } ?>
Fatal error: Class 'DatabaseObject' not found in C:\wamp\www\library\includes\user.php on line 5

Does anyone have any idea of what's going on???? 有谁知道发生了什么吗???? Everything works fine if the DatabaseObject class is in the same file as the user class thouth... not sure whats going on with the include. 如果DatabaseObject类与用户类thouth在同一个文件中,则一切正常,...不确定include怎么了。

I am very embarrassed to say that I was missing the ?php tag at the top of the DatabaseObject.php file. 我很尴尬地说我在DatabaseObject.php文件的顶部缺少?php标记。 Sorry for wasting everyone's time. 很抱歉浪费大家的时间。 :( :(

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM