Results 1 to 10 of 14
Thread: PHP
-
-
- Join Date
- 02-13-07
- Location
- Fort Worth, TX
- Posts
- 42,785
- Post Thanks / Like
- Blog Entries
- 5
-
-
-
-
- Join Date
- 10-21-08
- Location
- Waco, Texas
- Posts
- 12,228
- Post Thanks / Like
- Blog Entries
- 6
-
- Join Date
- 11-13-07
- Location
- Plano, TX and Ruston, LA
- Posts
- 32,364
- Post Thanks / Like
- Blog Entries
- 43
-
- Join Date
- 05-11-08
- Location
- Connecticut
- Posts
- 2,021
- Post Thanks / Like
- Blog Entries
- 4
-
11-07-11, 08:32 PM #9
Re: PHP
Well, since you volunteered!
What is the benefit of turning your database results into objects, like with PDO Statement Fetch Obj/class or using a composition pattern to turn it into an object, versus just Fetching the results into a normal 2 dimensional array and using the array?
As I'm making the OOP jump, that is one thing that has alluded me.
And finally, to brag, I spent today coding classes so I can write my sql statements in my logic layer like this and they're aliased from my database names:
// Will do an insert bc no conditionals :
// $result = $db->update( $sql->table('user')->set('userName','insert test2')->set('userPassword', 'aljkdajldajdlajfas') );
// A select:
// $result = $db->find( $sql->table('user')->select('user_id')->field('userName')->eq('aw')->order('lajdfa')->limit('1') );
// An Update:
// $result = $db->update( $sql->table('user')->set('userId', '1')->field('userId')->eq('2') );
For the code:
Sharing my knowledge of PHP, HTML, CSS, XML, Java and more. - My Database Classes
-
11-07-11, 09:16 PM #10
Re: PHP
dont, there isnt, avoid oop approches in php, most are generally pointless. Good php is php thats built for speed, speed comes from avoiding complicated code, complicated code generally comes from trying to generalize a problem too much so you can make an object out of it.
Especially trying to make a class that can write queries for you, which is silly (im putting that mildly), that code up there does not change, so why no just save php the work of having to go through that class 8-9 times per line just to generate a query that's static?You have to keep in mind that php is compiled on the fly when a page is called / required/included, every time a new initialization occurs . If i put that code on tpg on a gateway page, it'd be compiled nearly 900-1500 times a minute, thats a hell of a lot of work for generating a query thats gonna be run on a gateway page in addition to the actual work the page uses that query for.
Honestly, i'd say php methodology is closer to that of say... lisp than any OO language. Php does support classes, but most people come from oop languages and thus abuse the feature, always keep in mind that code is compiled on the fly, your goal should be a balanace of KISS code (stuff is hardcoded) and making code easy to write and read (objects and classes).
besides php has most of these things build straight in, PHP: MySQL Functions - Manual
you can though, write a class to wrap the mysql functions, which is especially usefully for persistent connections to db's. My db wrappers are simple, with only a few functions such as $result=$db->query_read("select stuff from table"), or $db->query_write("update table set field where condition"), usually a $db->num_row($result), which merly wraps mysql_num_rows,
finally a $row=$db->fetch_array($result), which wraps mysql_fetch_array i'd use like
PHP Code:$games=array();
$result = $db->query_read("SELECT id, name, abbrv, enabled FROM " . TABLE_PREFIX . "rsma_games ORDER BY name asc");
if ($db->num_rows($result) > 0)
{
while ($game = $db->fetch_array($result))
{
$games[$game['id']]=array(
'name'=>$game['name'],
'abbrv'=>$game['name'],
'enabled'=>(int) $game['enabled'], //expect mysql results to be returned as strings, so cast this to int
);
}
}
$games would be populated here
even that i code i posted i get annoyed by, as table_prefix is rather dumb too, but thats a different story, regardless its proper methodology and produces efficient code.
In addition to people forgetting that php is compiled during run time, people tend to forget that php, depending on the apache config is initalized many many times amongst apache children. For example, tpg is initalized several hundred times amongst all of apaches nodes. So its not just 1 instance of your program running, its hundreds, objects and classes only increase the amount of cpu and memory that ultimately ends up being used.Last edited by Bunni; 11-07-11 at 09:22 PM.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks