Php 从从mysqli扩展的类扩展类

Php 从从mysqli扩展的类扩展类,php,oop,class,mysqli,Php,Oop,Class,Mysqli,我不确定这是否可行,因为我还不太擅长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) {

我不确定这是否可行,因为我还不太擅长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\u database.php中 在线xx

警告:数据库::get_错误() [数据库.获取错误]:无法获取 搜索 C:\wamp\www\xxx\class\u database.php 第xx行

警告:mysqli::close() [mysqli.close]:无法获取搜索 在C:\wamp\www\xxx\class\u 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\u database.php 线

叹息


有什么想法吗?

因为
search
类中的
search
属性是非静态的,所以必须首先实例化一个对象:

$database = new database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
$search = new search("search term");
print_r($search->get_result());
或许你可以阅读一下PHP中关于OOP的基本教程,这是谷歌的第一个教程:

编辑:还有一些对
self::get_error()的调用
数据库
类中,应将这些方法重新编写为实例方法,而不是类方法:


$this->get_error()

数据库中调用
mysqli
类的方法时使用了错误的方法

在访问静态方法、静态属性、常量或要覆盖的当前方法的父级时,您只希望使用双冒号
。mysqli类中的任何方法都不是静态的,尽管php手册在方法列表中使用了

\uuu构造()
中使用
父级::\uu构造()
的方式是正确的,因为您使用的是要覆盖的父级的当前方法

但在其他方法中,您希望使用
$this->
引用其他父方法

fetch\u all()
fetch\u assoc()
中,使用
$this->query()
而不是
parent::query()

\uu destruct()
中使用
$this->close()取而代之


使用
self::get_error()时
,您可能希望将其更改为
$this->
,或者修改函数定义,使其保持静态,就像
公共静态函数get\u error()

我认为从
数据库扩展类可能是个坏主意

以下是我的解决方案:

class search
{

    public $mysqli = null;

    public function __construct($mysqli)
    {
       $this->mysqli = $mysqli;
    }

    public function get_result($parameter)
    {
        $sql = "
        SELECT *
        FROM root_contacts_cfm
        WHERE root_contacts_cfm.cnt_id = '".$parameter."'
        ORDER BY cnt_id DESC
        ";

        $item = $this->mysqli->fetch_assoc($sql);
        return $item;
    }
}
将对象
数据库
类传递到
搜索
类中,因此我可以通过以下方式调用此搜索类:

$mysqli = new database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
$output = new search($mysqli);
print_r($output->get_result('1'));

我说得对吗…?

谢谢。我刚刚更改了它们并测试了类,但这次我得到了这个错误:
致命错误:达到最大函数嵌套级别“100”,正在中止!在C:\wamp\www\xx\class_database.php中,第385行
引用了
数据库
类别中的这段代码:
public function query($query){$result=$this->query($query);if($result){return$result;}else{return this->get_error();}
您将要使用
parent::query()
因为您所在的函数正在覆盖父查询()