简体   繁体   English

简单的PHP OOP和MySQLI

[英]Simple PHP OOP and MySQLI

I am trying to learn some more PHP, OOP and MySQLI, but I am stuck. 我正在尝试学习更多的PHP,OOP和MySQLI,但是我陷入了困境。 Google didn't help me and searching on this website didn't give me any results either. Google并没有帮助我,在这个网站上搜索也没有给我任何结果。 I have a config class to connect to the database, and in another class I want to run some queries, but I am getting this error with whatever I am trying: 我有一个配置类可以连接到数据库,在另一个类中,我想运行一些查询,但是无论尝试什么,都会遇到此错误:

Fatal error: Call to a member function query() on a non-object in test.php on line 9 致命错误:在第9行的test.php中的非对象上调用成员函数query()

Can someone please help me? 有人可以帮帮我吗?

config.php: config.php:

<?php

class Database
{
    private $host;
    private $user;
    private $password;
    private $db;
    private $mysqli;


    function __construct() {

        $this->host     = "private";
        $this->user     = "private";
        $this->pass     = "private";
        $this->data     = "private";

        $this->mysqli   = new mysqli($this->host, $this->user, $this->pass, $this->data);
    }


    public function query($query)
    {
        return $this->mysqli->query($query);
    }
}
?>

test.php: test.php:

<?php
class Dummy
{
    private $Database;

        function __construct()
    {
        $this->Database = new Database();
        $this->Database->test = $this->mysqli->query("SELECT test FROM test")->fetch_object()->test;
        }
}
?>

index.php: index.php:

<?php

require 'config.php';
require 'test.php';

$test = new Database();
$test = new Dummy();

echo $this->Database->test;
?>

How about using Dependency injection, create the Database object then pass that as a parameter to the class through the construct. 如何使用依赖注入,创建Database对象,然后将其作为参数通过构造传递给类。

<?php
class Database
{
    private $host;
    private $user;
    private $password;
    private $db;
    private $mysqli;


    function __construct($host,$user,$pass,$data) {

        $this->host     = $host;
        $this->user     = $user;
        $this->pass     = $pass;
        $this->data     = $data;
        $this->mysqli   = new mysqli($this->host, $this->user, $this->pass, $this->data);
    }

    public function query($query)
    {
        return $this->mysqli->query($query);
    }
}
?>

... ...

<?php
class Dummy
{

    function __construct(Database $db)
    {
        $this->db = $db;
    }

    function get_test_yada(){
        return $this->db->mysqli->query("SELECT test FROM test")->fetch_object()->test;
    }
}
?>

... ...

<?php 
$db    = new Database('127.0.0.1','root','pass','database');
$dummy = new Dummy($db);


$dummy->get_test_yada();
?>

Untested, hope it helps 未经测试,希望对您有所帮助

For the goodness sake, DON'T learn them all at once. 出于善意,请勿立即学习所有内容。 You won't get any sensible result. 您不会得到任何明智的结果。

Each is a hard and complex topic alone, of which OOP is hardest. 每个都是单独的难题,而OOP则是最困难的。

For the database interacton use PDO prepared statements. 对于数据库交互,请使用PDO准备的语句。

As the OOP practice, never extend class from a service, but use it as a class variable. 作为OOP的实践,切勿从服务扩展类,而应将其用作类变量。

class Dummy
{
    protected $db;

    function __construct($pdo)
    {
        $this->db = $pdo;
    }

}
$this->mysqli   = new mysqli($this->host, $this->user, $this->pass, $this->data);

If connecting fails, you can't call anything on that member variable. 如果连接失败,则无法对该成员变量进行任何调用。 Check if your connection is established first. 检查是否首先建立连接。

And, if you want to have access to that variable, you should make it public. 而且,如果您想访问该变量,则应将其公开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM