【发布时间】:2011-02-22 22:24:03
【问题描述】:
我不确定这是否可行,因为我还不太擅长 OOP 编程。
我有这个从 mysqli 扩展的 db 类,
class database extends mysqli
{
# overwrite parent __construct
public function __construct($hostname = null,$username = null,$password = null,$database = null,$port = null, $socket = null)
{
$hostname = $hostname !== null ? $hostname : ini_get("mysqli.default_host");
$username = $username !== null ? $username : ini_get("mysqli.default_user");
$password = $password !== null ? $password : ini_get("mysqli.default_pw");
$database = $database !== null ? $database : "";
$port = $port !== null ? $port : ini_get("mysqli.default_port");
$socket = $socket !== null ? $socket : ini_get("mysqli.default_socket");
parent::__construct($hostname,$username,$password,$database,$port,$socket);
# check if connect errno is set
if (mysqli_connect_errno())
{
throw new RuntimeException('Cannot access database: ' . mysqli_connect_error());
}
}
# fetches all result rows as an associative array, a numeric array, or both
# mysqli_fetch_all (PHP 5 >= 5.3.0)
public function fetch_all($query)
{
$result = parent::query($query);
if($result)
{
# check if mysqli_fetch_all function exist or not
if(function_exists('mysqli_fetch_all'))
{
# NOTE: this below always gets error on certain live server
# Fatal error: Call to undefined method mysqli_result::fetch_all() in /.../class_database.php on line 28
return $result->fetch_all(MYSQLI_ASSOC);
}
# fall back to use while to loop through the result using fetch_assoc
else
{
while($row = $result->fetch_assoc())
{
$return_this[] = $row;
}
if (isset($return_this))
{
return $return_this;
}
else
{
return false;
}
}
}
else
{
return self::get_error();
}
}
# fetch a result row as an associative array
public function fetch_assoc($query)
{
$result = parent::query($query);
if($result)
{
return $result->fetch_assoc();
}
else
{
# call the get_error function
return self::get_error();
# or:
# return $this->get_error();
}
}
public function query($query)
{
$result = $this->query($query);
if($result)
{
return $result;
}
else
{
return $this->get_error();
}
}
...
# display error
public function get_error()
{
if($this->errno || $this->error)
{
return sprintf("Error (%d): %s",$this->errno,$this->error);
}
}
public function __destruct()
{
parent::close();
//echo "Destructor Called";
}
}
我有这种程序风格的代码,我想把它变成一个从上面的数据库类扩展的类,
if(isset($_REQUEST['search'])) $search = $_REQUEST['search'];
$sql = "
SELECT *
FROM root_pages
WHERE root_pages.pg_cat_id = '2'
AND root_pages.parent_id != root_pages.pg_id
AND root_pages.pg_hide != '1'
AND root_pages.pg_url != 'cms'
AND root_pages.pg_content_1 LIKE '%".$search."%'
OR root_pages.pg_content_2 LIKE '%".$search."%'
AND root_pages.pg_content_1 NOT LIKE '%http://%'
AND root_pages.pg_content_2 NOT LIKE '%http://%'
ORDER BY root_pages.pg_created DESC
";
$items = $connection->fetch_all($sql);
$total_item = $connection->num_rows($sql);
所以我认为,理论上我可以将此代码扩展为这样的类,
class search extends database
{
public $search = null;
public function __construct($keyword)
{
$this->search = $keyword;
}
public function get_result()
{
$sql = "
SELECT*
FROM root_pages
WHERE root_pages.pg_cat_id = '2'
AND root_pages.parent_id != root_pages.pg_id
AND root_pages.pg_hide != '1'
AND root_pages.pg_url != 'cms'
AND root_pages.pg_content_1 LIKE '%".$this->search."%'
OR root_pages.pg_content_2 LIKE '%".$this->search."%'
AND root_pages.pg_content_1 NOT LIKE '%http://%'
AND root_pages.pg_content_2 NOT LIKE '%http://%'
ORDER BY root_pages.pg_created DESC
";
$items = parent::fetch_all($sql);
return $items;
}
}
然后我调用搜索的对象,
$output = new search('1');
print_r($output->get_result());
但我得到了很多错误,
警告:mysqli::query() [mysqli.query]:无法获取搜索 在 C:\wamp\www\xxx\class_database.php 在第 xx 行
警告:数据库::get_error() [database.get-error]:无法获取 搜索 C:\wamp\www\xxx\class_database.php 上 第 xx 行
警告:mysqli::close() [mysqli.close]:无法获取搜索 在 C:\wamp\www\xxx\class_database.php 在第 xx 行
我做错了什么?我该如何解决?
谢谢。
编辑:
当我尝试以这种方式从父类(数据库)调用子类(搜索)时,
$database = new database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
print_r(search::get_result());
然后我得到这个错误,
致命错误:非静态方法 mysqli::query() 不能被调用 静态地 C:\wamp\www\xxx\class_database.php 上 线
叹息...
有什么想法吗?
【问题讨论】: