简体   繁体   English

PHP的oop和mysql

[英]php oop and mysql

I need to get data, to check and send to db. 我需要获取数据,以检查并发送给db。 Programming with PHP OOP. 用PHP OOP编程。

Could you tell me if my class structure is good and how dislpay all data?. 您能告诉我我的班级结构是否良好,以及如何分配所有数据吗? Thanks 谢谢

<?php
class Database{

    private $DBhost = 'localhost';
    private $DBuser = 'root';
    private $DBpass = 'root';
    private $DBname = 'blog';

    public function connect(){
        //Connect to mysql db
    }

    public function select($rows){
        //select data from db
    }

    public function insert($rows){
        //Insert data to db
    }

    public function delete($rows){
        //Delete data from db
    }
}

class CheckData{

    public $number1;
    public $number2;

    public function __construct(){
        $this->number1 = $_POST['number1'];
        $this->number2 = $_POST['number2'];
    }

    function ISempty(){
        if(!empty($this->$number1)){
            echo "Not Empty";

            $data = new Database();
            $data->insert($this->$number1);
        }
        else{
            echo "Empty1";
        }

        if(!empty($this->$number2)){
            echo "Not Empty";

            $data = new Database();
            $data->insert($this->$number2);
        }
        else{
            echo "Empty2";
        }       
    }
}

class DisplayData{

    //How print all data?
    function DisplayNumber(){
        $data = new Database();
        $data->select();
    }   
}

$check = new CheckData();
$check->ISempty();

$display = new DisplayData()
$display->DisplayNumber();
?>

That's horrible piece of code. 那是一段可怕的代码。

  1. For database communication use PDO . 对于数据库通信,请使用PDO It's not perfect, but it's fine. 这不是完美的,但是很好。
  2. Every time you'll need database you'll connect with that db? 每次您需要数据库时都将与该数据库连接?
  3. Database::insert() etc. would have to be a true magician to guess where its parameter should be inserted Database::insert()等必须是一名真正的魔术师才能猜测应该在何处插入其参数

You need a better IDE to show your undefined variables. 您需要一个更好的IDE来显示未定义的变量。 I use PHPStorm . 我使用PHPStorm

You need some dependency injection . 您需要一些依赖注入 Use encapsulation . 使用封装 Just follow SOLID . 只需遵循SOLID即可

Second to last thing, don't echo from within objects. 倒数第二件事,不要从对象内部回显。 You can't unit test effectively your code that way. 您不能以这种方式有效地对代码进行单元测试

You might also try some TDD . 您也可以尝试一些TDD

You're going at things wrong here, I think. 我认为您在这里出了错。 What you are constructing is a wrapper around already-existing database functions. 您正在构造的是对已经存在的数据库函数的包装。 Thing is, those functions already defined to do exactly what you want - the object oriented approach here serves no real purpose since it can only connect to one database and the methods are not really well defined and seem to act as pure pass-throughs to the underlying layer. 事实是,那些已经定义为完全可以实现您想要的功能的方法-此处的面向对象方法没有真正的目的,因为它只能连接到一个数据库,并且这些方法的定义不是很好,并且似乎只是传递给数据库。底层。

As others have mentioned, the code you seem to be going at here is not going to suit your purposes. 正如其他人提到的那样,您似乎要在此处使用的代码不适合您的目的。 Something like PDO will serve you well and its already built in to PHP. 诸如PDO之类的东西将很好地为您服务,并且它已经内置在PHP中。 You don't need to re-invent the wheel for such common code as accessing a database. 您不需要为访问数据库之类的通用代码而费力工作。

Learning a few core Object Orientation principles will do you good too - look at encapsulation and code re-use first. 学习一些核心的面向对象原理也将对您有好处-首先看一下封装和代码重用。 They'll help in the future when you have to maintain that code you write! 在将来您必须维护编写的代码时,它们将为您提供帮助!

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

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