问题
I have 2 classes (2 files)
db.class.php
user.class.php
I want to use some functions from db.class.php
:
db.class.php
Class DBManager {
/** all functions goes here ...*/
}
$DB = new DBManager();
The content of user.class.php is:
Class User extends DBManager {
function User() {
}
function Total($table) {
$query = $DB->Execute("SELECT * FROM $table");
$total = $DB->NumRows($query);
return $total;
}
}
$User = new User();
When I want to use my new function total($table) I get 2 errors:
Undefined variable: DB in ..\class\user.class.php on line 14
Fatal error: Call to a member function Execute() on a non-object in ..\class\user.class.php on line 14
I includes the 2 classes in my main.php file like:
include 'class/db.class.php'; include 'class/user.class.php';
Edit 1:
Related post: best trick when using an extending class (PHP)
回答1:
I suppose you want to call the parent function so try somenthing like that
function Total($table) {
$query = parent::Execute("SELECT * FROM $table");
$total = parent::NumRows($query);
return $total;
}
And if it doesnt't work try with this
instead of parent
PHP parent codumentation
回答2:
Well, yes, $DB
is not defined/in scope within the Total
function.
If $DB
is a property of the class, you'll need to access it with $this->DB
.
Update after update:
You're instantiating the DBManager
into the global variable $DB
. This variable is only in scope in the global scope. It is not automatically available in any other scope, like inside functions. Whether or not User
inherits from DBManager
is completely irrelevant, the instantiated variable $DB
is not in scope inside any function.
There's a lazy way to access variables in the global
scope, but I'm not going to mention that here. The proper way would be to instantiate $DB
as a class member of User
or pass it upon instantiating User
:
class User {
protected $DB = null;
public function __construct($DB) {
$this->DB = $DB;
}
public function Total() {
$this->DB->…
}
}
$DB = new DBManager();
$user = new User($DB);
回答3:
What is that $DB? You never set that to anything, so even if you include those files, you cannot reach that variable
Depending on your db.class.php, you probably need to instantiate some sort of $DB like this
$DB = new db();
(either in the function itself, or in a constructor for the user class using $this->DB
)
try something like this:
Class User extends DBManager {
private $DB;
function User() { // should be __construct() really
$this->DB = new DBManager();
$this->DB->Connect();
}
function Total($table) {
$query = $this->DB->Execute("SELECT * FROM $table");
$total = $this->DB->NumRows($query);
return $total;
}
}
$User = new User();
回答4:
Looks like you're confused about how inheritance works. The sub-class inherits the public and protected members and methods of the parent class. Therefore, in the example you've given, you should probably be using $this->Execute("SELECT * FROM $table")
and $this->NumRows($query)
. Although the Execute
and NumRows
functions aren't defined in the User
class, they are in the DBManager
class, so you access them as if they were defined in User
.
回答5:
The solution for my issue is:
$query = DBManager::Execute("SELECT * FROM $table");
$total = DBManager::NumRows($query);
来源:https://stackoverflow.com/questions/4836401/problems-when-extending-a-php-class