#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on May 9, 2012 20:50:42 GMT -8
Another fun PHP debate to bring up.
I have a MySQL database that has tables for parts of my website, such as user accounts, content they post, categories for that content, etc.
In early generations I used arrays for all my MySQL results, so I might have $user['name'], $user['id'], and $user['location']. Maybe my user has a custom name colour, so I might also have $user['color'].
However, I went through a nice transition in later generations to the object oriented world of programming. Now I have $user->name, $user->id, $user->location, and $user->color (all encapsulated, of course).
Now each has it's usual advantages and disadvantages. Arrays are simple and quick. Run a function or two on my DB results, and I have a user. However, the array does have a downfall. I have to print their name color every time I print the name:
<span style='color:#<?php echo $user['color']; ?>;'><?php echo $user['name']; ?></style>
Objects on the other hand make all of this simple:
class User { public function printName() { echo '<span style="color:#'.$this->color.';">'.$this->name.'</style>'; } }
Now all I have to call is $user->printName();, however getting the database data into the User class is a long process (I have written hundreds of lines of code in multiple generations for just this task).
My question to the programmers here:
Which do you prefer, and why?
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on May 10, 2012 14:21:20 GMT -8
function printName($user) { echo '<span style="color : #', $user['color'], '">', $user['name'], '</span>'; }
Or $user['name-color'] = '<span style="color : #' . $user['color'] . '">' . $user['name'] . '</span>';
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on May 10, 2012 14:24:35 GMT -8
Is this preferred over OO? If so, why?
|
|
inherit
97216
0
Nov 26, 2024 13:53:14 GMT -8
Bennett 🚀
Formerly iPokemon.
3,622
January 2007
catattack
iPokemon's Mini-Profile
|
Post by Bennett 🚀 on May 10, 2012 15:33:06 GMT -8
I prefer arrays because, as Charles did, you can do custom functions that you can use for just about any results that are created to the variable.
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on May 10, 2012 15:37:16 GMT -8
To extend the possibilities even more, I am trying to replicate this sort of method to see how it works for the same problem. My plan is to make each object from the DB extend collection (via another class), so that I can assign them as arrays, but still put methods in them like objects.
|
|
inherit
97216
0
Nov 26, 2024 13:53:14 GMT -8
Bennett 🚀
Formerly iPokemon.
3,622
January 2007
catattack
iPokemon's Mini-Profile
|
Post by Bennett 🚀 on May 10, 2012 15:39:30 GMT -8
That is pretty efficient. I'm just a little noob compared to you guys in PHP though, so I don't really have much leverage to say anything haha.
EDIT: PDO has a fetchClass method or something in it that will send the data to a class and execute it. I thought that was pretty cool.
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on May 10, 2012 15:56:11 GMT -8
That is pretty efficient. I'm just a little noob compared to you guys in PHP though, so I don't really have much leverage to say anything haha. EDIT: PDO has a fetchClass method or something in it that will send the data to a class and execute it. I thought that was pretty cool. I know little about PDO, as I have always used MySQL (well actually MySQLi). I will have to look into that though! My issue is less getting things into an array form, and more creating objects from those arrays.
|
|
inherit
97216
0
Nov 26, 2024 13:53:14 GMT -8
Bennett 🚀
Formerly iPokemon.
3,622
January 2007
catattack
iPokemon's Mini-Profile
|
Post by Bennett 🚀 on May 10, 2012 16:03:47 GMT -8
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on May 10, 2012 16:06:25 GMT -8
That is a perfect example, thanks! I am using MySQLi right now, but I may look into PDO in the future. I am writing this software with a modular DB system though, so all I will have to do is write a new DB class to implement PDO if I choose to use it.
|
|
inherit
97216
0
Nov 26, 2024 13:53:14 GMT -8
Bennett 🚀
Formerly iPokemon.
3,622
January 2007
catattack
iPokemon's Mini-Profile
|
Post by Bennett 🚀 on May 10, 2012 16:08:48 GMT -8
Yeah, I recently delved into PDO because a friend suggested I gain some ground in the unknown world of database access classes haha. It's pretty straightforward, it's all OO, and follows mysqli almost to a t, other than the procedural style is non-existant in PDO.
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on May 11, 2012 10:15:10 GMT -8
Is this preferred over OO? If so, why? I personally hate OO, and I hate it especially in PHP. Also, objects take up more memory. If it's not a large collection of functions, I don't see why it would benefit to be an object. Generally, the benefits to OO is that it's portable and extensible. You can move it from one project to another by just copying a file and not worrying about function or variable names clashing. When you're working with a single function like this, that's not an issue. So the extensible and portable benefits of OO no longer outweigh the extra memory use and harder-to-read (longer) code. Just my two cents. Some people are die-hard OO fans, though. EDIT: You may want to run checks for how much slower/faster this runs as an alternative, but you should be able to do: SELECT `name`, `color`, CONCAT('<span style="color : ", `color`, ';">', `name`, '</span>') AS `formatted` ETC Then just $user['formatted']
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on May 11, 2012 10:18:53 GMT -8
Is this preferred over OO? If so, why? I personally hate OO, and I hate it especially in PHP. Also, objects take up more memory. If it's not a large collection of functions, I don't see why it would benefit to be an object. Generally, the benefits to OO is that it's portable and extensible. You can move it from one project to another by just copying a file and not worrying about function or variable names clashing. When you're working with a single function like this, that's not an issue. So the extensible and portable benefits of OO no longer outweigh the extra memory use and harder-to-read (longer) code. Just my two cents. Some people are die-hard OO fans, though.. Definitely points to pay attention to. I like OO over-all, and it feels like a more orderly way to do things to me, but I agree that it easily becomes harder to read (especially when certain (not me, of course ) developers choose not to comment their code well). I love modularity, however I am also not sure how much fo the code I am writing will ever be used outside of my own internal projects
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on May 11, 2012 10:21:53 GMT -8
Be sure to check my EDIT.
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on May 11, 2012 10:23:46 GMT -8
Be sure to check my EDIT. Fancy! ;D
|
|