Welcome Guest, Not a member yet? Register   Sign In
quick question.
#1

[eluser]jdfwarrior[/eluser]
I have caught myself creating shortcut to libraries to keep from having to type so much and to eliminate the possibility of making more typos. For instance:

Code:
$db = $this->db;

$db->get('my_table');

Is there anything that would categorize this as bad practice/something I SHOULD NOT do?
#2

[eluser]pistolPete[/eluser]
You are copying the db object which means more memory is consumed.

If you want to use a shortcut, use this:
Code:
// note the '=&', which means assigning by reference
$db =& $this->db;

This may be irrelevant if you use PHP5 since it has a different copy handling: http://www.php.net/manual/en/migration5.oop.php
#3

[eluser]TheFuzzy0ne[/eluser]
I use alias's all the time. Just step back and ask yourself "Is it obvious what's going on here?" If it is, you should be fine. I try (but often fail) to code in a way that's easy to follow, so another developer could take over the project without problems.

It is possible to use too many aliases, so I'd suggest you use them sparingly.

The alias you have above (in my humble opinion), needs to be set by reference. I've heard that PHP does it automatically, but I've yet to see it documented anywhere, so I'd do this:
Code:
$db =& $this->db;

$db->get('my_table');

EDIT: Fudge!
#4

[eluser]jdfwarrior[/eluser]
Yeah good call I had not even thought about that...
#5

[eluser]TheFuzzy0ne[/eluser]
Thanks, Pete! You answered my question too. Smile
#6

[eluser]Colin Williams[/eluser]
In PHP 5 objects are always referenced. You need to use the clone() function to get a copy of the instance.making all these aliases just seems messy and pointless
#7

[eluser]brianw1975[/eluser]
[quote author="jdfwarrior" date="1237243662"]I have caught myself creating shortcut to libraries to keep from having to type so much and to eliminate the possibility of making more typos. For instance:

Code:
$db = $this->db;

$db->get('my_table');

Is there anything that would categorize this as bad practice/something I SHOULD NOT do?[/quote]


As a warning (that others skirted around) this will totally crash and not work in PHP4.

I develop at home with php5 but my production server is PHP4 - yes, I am lazy and haven't upgraded it yet. So trust me, it will kill it, at least it did in mine. (probably because i didn't know about the clone() method mentioned above.
#8

[eluser]Colin Williams[/eluser]
If you are on the fence about an implementation and your only justification is to "type less when coding," then it's probably not a worthy implementation. There are cases for keeping APIs short and to the point (like having a "db" class and not a "database" class, for instance), but what you're doing is a tad bit superfluous.




Theme © iAndrew 2016 - Forum software by © MyBB