2

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();
?>
1
  • Class structure is good, however, one oops I see. "ISempty" method probably should be "isEmpty" as to avoid mistakes by others reading the code. I initially thought you were referencing an interface when I saw "ISempty" (interface named "Sempty" lol). Also, I'd probably abstract the Database class myself and make it reusable through any project (without recoding the db credentials). Commented May 27, 2010 at 18:47

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

4 Comments

All very good of course, but to learn in one go is pretty daunting. I suggest learning about encapsulation first off since it is a part of all the other things you mention. Unit testing, dependency injection and the general SOLID principles follow on from there. Also, NetBeans has a very nice PHP IDE these days and is totally free.
My previous IDE is/was NetBeans. Storm handles deployment to a SCM and also to a server via ftp.
I believe NetBeans has remote FTP push functionality now, and I've been working fine with Git and SVN in it for a while now - free is always good, yes!
Do you have a problem with xdebug?
1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.