Welcome Guest, Not a member yet? Register   Sign In
Objects: Why are they useful?
#16

[eluser]Edemilson Lima[/eluser]
Back to 2001, I didn't realize how useful objects are since I started to code multiples calls to different databases without them. Before, I must setup a lot of variables for the same things:

Code:
$host1='localhost';
$user1='mydbuser';
$pass1='mydbpass';
$base1='mybase';
$host2='192.168.1.100';
$user2='otherdbuser';
$pass2='otherdbpass';
$base2='otherbase';

$link1=mysql_connect($host1,$user1,$pass1);
if($link1) {
  mysql_select_db($base1,$link1);
  $query1="select * from table1";
  $result1=mysql_query($query1,$link1);
  if(mysql_num_rows($result1)) {
    $link2=mysql_connect($host2,$user2,$pass2);
    mysql_select_db($base2,$link2);
    if($link2) {
      while($row1=mysql_fetch_array($result1)) {
        $query2="update table2 set field='value' where id='$row1->[id]'";
        mysql_query($query2,$link2);
      }
    }
  }
}

Well, we can simplify a little bit the code above with functions, but after I have made my "database" class, look how the mess above become:

Code:
$db1=new database($host1,$user1,$pass1,$base1);
$db2=new database($host2,$user2,$pass2,$base2);
$db1->db_query("select * from table1");
if($db1->db_rows()) {
  while($db1->db_fetch()) {
    $db2->db_query("update table2 set field='value' where id='$db1->[id]'");
  }
}

As you can see, is much simpler do the things with objects and we can focus on what really matters: the application logic. And also, the database class can check if the connection was made before or not and connects automatically. It can create tables, benchmark the queries, send database errors by Email, check if a table exist, use data compression for remote server connections (MYSQL_CLIENT_COMPRESS), count the number of rows returned by a query, store the last id generated, store the number of rows that was affected, format returned data, free results, repair tables, etc, etc. The functions works together and exchange data nicely, preventing errors and making the appropriate checks. Thanks to the objects properties and methods, all of this is possible. And the best of all, you only write it once and spawn how many objects from it you need.


Messages In This Thread
Objects: Why are they useful? - by El Forum - 01-22-2008, 02:59 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 03:18 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 03:32 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:02 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:20 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:22 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:31 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:38 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:41 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:49 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:51 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:51 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:52 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 05:01 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 05:06 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 05:19 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 06:55 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 07:15 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 07:23 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 07:23 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 08:15 PM
Objects: Why are they useful? - by El Forum - 01-24-2008, 05:47 PM
Objects: Why are they useful? - by El Forum - 01-24-2008, 05:59 PM
Objects: Why are they useful? - by El Forum - 01-24-2008, 06:34 PM
Objects: Why are they useful? - by El Forum - 01-25-2008, 02:34 AM



Theme © iAndrew 2016 - Forum software by © MyBB