简体   繁体   中英

php oop and mysql

I need to get data, to check and send to db. Programming with 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 . 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

You need a better IDE to show your undefined variables. I use PHPStorm .

You need some dependency injection . Use encapsulation . Just follow 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 .

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. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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