Extending MySQLi
I've run into problems extending the MySQLi class. It won't let me add any properties.
class MySQLii extends MySQLi {
public $database;
public function MySQLii($host, $username, $password, $database){
// Initialize MySQLi
parent::MySQLi($host, $username, $password, $database);
// Save database name
$this->database = $database;
}
}
$mysqlii = new MySQLii('localhost', 'root', 'password', 'database');
var_dump($mysqlii);
object(MySQLii)#1 (17) {
["affected_ro开发者_Python百科ws"]=> int(0) ["client_info"]=> string(48) "mysqlnd 5.0.5-dev - 081106 - $Revision: 289630 $" ["client_version"]=> int(50005) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["field_count"]=> int(0) ["host_info"]=> string(42) "MySQL host info: Localhost via UNIX socket" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(6) "5.1.44" ["server_version"]=> int(50144) ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(4019) ["warning_count"]=> int(0) }
Note the absence of the database
property I added in the MySQLii constructor. Am I missing something?
Apparently extending the MySQLi class makes it impossible to add additional properties. Bad PHP. Bad.
Hm, either I am missing something or you named your constructor MySQLii
instead of __construct
. (This is not Java ;))
Renaming it appropriately could solve the problem as it looks as if your constructor is not called.
I've just made a wrapper to MySQLi
and MySQLi_STMT
by extending both classes (as per PHP 5.3 and 5.2). I can confirm that while the variables don't appear when you dump it with var_dump
, the class methods accesses the variables like any other normal object without problems.
class Foo extends MySQLi
{
public $var = 'blah';
public function blah()
{
echo $this->var;
}
}
$foo = new Foo();
echo $foo->var;
$foo->blah();
Accessing the variable from outside of the class also works as expected. As long as you don't normally use var_dump
to figure out which variable exists or other unorthodox functionality, just code as you normally do and you'll have no problems.
精彩评论