English 中文(简体)
PHP - 无法从其他类的 Mysqli 类中引用方法
原标题:PHP- Cannot reference method from mysqli class in another class

我试图继承所有的方法 和属性 从超全球的Mesqli 类到我的DB类。

这是DB类:

class DB extends mysqli
{
    protected $mysqli;

    public function __construct () {
        // connect to MySQL
        $mysqli = new mysqli( host ,  username ,  password ,  dbname );

        // output error if unable to connect
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " .     $mysqli->connect_error;
            exit;
        }
    }
}

以下是主要的会议课:

class Meetings
{

    function __construct () {
        require_once( ../include/classes/db.class.php );
        $db = new DB();

        $field = $db->real_escape_string($_POST[ about ]);

    }

}

在会议课上,我想通过即时DB课 来称呼真实的越野字符串方法 由超级全球的Mesqli课继承

我得到这个错误 : 警告: Mysqli:: real_ escape_string () [mysqli. real- escape- string]: 无法获取 DB

问题回答

Oops! You created the DB object, and the db object creates an another instance of the mysqli class. You should not call new mysqli, but you should call

parent::__construct( host ,  username ,  passw或d ,  dbname )

    parent::mysqli( host ,  username ,  passw或d ,  dbname )

I don t know which is the good f或mat f或 the mysqli construct或.

在此之后, 您不需要 DB 类中的 Mysql 属性, 因为在这种情况下, 您的 Mysqli 属性等于 DB 类的美元 。 所以, 不是 $mysqli- gt; connect_ errno, 而是$this- gt; connect_ errno 是正确的 。





相关问题
Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

C++ Class Inheritance problem

Hi I have two classes, one called Instruction, one called LDI which inherits from instruction class. class Instruction{ protected: string name; int value; public: Instruction(string ...

Overloading a method in a subclass in C++

Suppose I have some code like this: class Base { public: virtual int Foo(int) = 0; }; class Derived : public Base { public: int Foo(int); virtual double Foo(double) = 0; }; ...

Embedding instead of inheritance in Go

What is your opinion of this design decision? What advantages does it have and what disadvantages? Links: Embedding description

Extending Flex FileReference class to contain another property

I want to extend the FileReference class of Flex to contain a custom property. I want to do this because AS3 doesn t let me pass arguments to functions through event listeners, which makes me feel sad,...

Interface Inheritance in C++

I have the following class structure: class InterfaceA { virtual void methodA =0; } class ClassA : public InterfaceA { void methodA(); } class InterfaceB : public InterfaceA { virtual ...

热门标签