TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Tips & Tricks (http://www.talkphp.com/tips-tricks/)
-   -   [Tip] Variable Prefixes (http://www.talkphp.com/tips-tricks/1032-tip-variable-prefixes.html)

bluesaga 09-06-2007 11:26 PM

[Tip] Variable Prefixes
 
Something i learnt, fairly recently but has already been a huge timesaver, and keeps scripts to a strict standardised programming method is variable prefixes.

Variable prefixes are 1 to 2 characters prior to the variable name, in the variable, for example:
$szString instead of $string.

The following are prefixes i use:

m_ Member, this is used as a pre-prefix to show a member variable and is used like: $this->m_iInteger;
g_ Global, this is used as a pre-prefix to show a global variable and is used like: global $g_aGlobal;
a Array ($aArray)
sz String ($szString, $sz looks better than $s before you ask)
i Integer ($iInteger)
f Float ($fFloat)
b Boolean ($bBoolean)
p Object/Pointer ($pPointer)
m Mixed (i.e. could be any value - $mMixed)

Think of the following peices of code:
PHP Code:

<?php
$numbers 
= array(151015);
$count count($numbers);
$addition $numbers[1]+$numbers[2];
$division $numbers[1] / $numbers[2];
$integer $addition+$division;
?>

With the previous snippet of code, you may of noticed that i have thought the $integer would be an integer, but looking a little closer you would notice that division could cause non-integer numbers.

Now if you take the following peice of code:

PHP Code:

$aNumbers = array(151015);
$iCount count($aNumbers);
$iAddition $aNumbers[1]+$aNumbers[2];
$fDivision $aNumbers[1] / $aNumbers[2];
$iInteger $iAddition+$fDivision

Now it is made very clear that the $iInteger would not be an integer, due to the $fDivision clearly not being an integer, thus causing a float result which could cause problems within your application.


This is obviously a very simple example of the benefits of variable prefixes, but imagine using these within multiple thousand line applications, with variables being used across multiple includes, variable prefixes come in much more handy then! Other benefits include, the ability to type-cast/type-enforce and definately readability!

Haris 09-06-2007 11:30 PM

Thanks Bluesaga.

I always wondered what sz stands for.

bluesaga 09-06-2007 11:41 PM

No problem, it is a little hard to get used to, from your normal programming, but since i have got use to it, its a lifesaver! Honestly

Wildhoney 09-06-2007 11:44 PM

Just to elaborate on Bluesaga's point about the automatic typecasting. I wrote a function earlier to automatically extract the prefix from GET, POST, COOKIE and SESSION arrays. You can then enforce the types how you wish - automatically.

It's common knowledge that most sites are hacked because a user is allowed to set the values of GET and POST (And less so COOKIE) to whatever they desire and thus causing all sorts of problems such as SQL injection attacks.

PHP Code:

function getPrefix($szPointer)
{
    
$aVariable str_split($szPointer);
    
$szPrefix '';
    
    foreach(
$aVariable as $szVariable)
    {
        
$szOrd ord($szVariable);
        
        if(
$szOrd >= 65 && $szOrd <= 90)
        {
            break;
        }
        
        
$szPrefix .= $szVariable;
    }
    
    return 
$szPrefix;


This function will return the prefix. I would use it the following scenario:

PHP Code:

function g($szPointer)
{
    
$szValue $_GET[$szPointer];
    
    switch(
getPrefix($szPointer))
    {
        
/* Code Typecasting */
    
}


The switch will contain the array pointer's prefix, such as sz or i. I would recommend then using the CTYPE_* functions.

localhost 09-07-2007 06:26 AM

Wow thanks both of you, I never knew anything about this so I will be reading again tonight when I get home from school and will hopefully learn alot of information about it, Thanks again!

Salathe 09-07-2007 10:36 AM

Quote:

Originally Posted by Wildhoney (Post 1854)
PHP Code:

function getPrefix($szPointer)
{
    
$aVariable str_split($szPointer);
    
$szPrefix '';
    
    foreach(
$aVariable as $szVariable)
    {
        
$szOrd ord($szVariable);
        
        if(
$szOrd >= 65 && $szOrd <= 90)
        {
            break;
        }
        
        
$szPrefix .= $szVariable;
    }
    
    return 
$szPrefix;



I'm just wondering why you went the route of split/foreach when a simple RegEx would be more readable (personally, I find it easier to glance at a pattern than work through a sequence of loops) and probably quicker? Also, in the case of 'pointers' without an uppercase character (e.g. random) the full string would be returned -- I'd have thought that in that sort of case, there'd logically be no prefix.

Karl 09-07-2007 10:54 AM

I agree that the RegEx could probably be more readable (if you know a bit of RegEx, that is), however, I was always under the impression that you can't beat regular loops in terms of speed. Afterall, by looping yourself you know exactly how many loops are required. In all honesty though, I have no idea how many loops a RegEx makes. This question probably deserves a new thread to itself, what is quicker, custom loops or RegEx :)

Salathe 09-07-2007 12:18 PM

Quote:

Originally Posted by Karl (Post 1871)
I agree that the RegEx could probably be more readable (if you know a bit of RegEx, that is), however, I was always under the impression that you can't beat regular loops in terms of speed.

PHP Code:

// My version of Wildhoney's getPrefix function
function getPrefixSalathe($szPointer)
{
    if (
preg_match('/^([a-z_]+)[A-Z]?/'$szPointer$aVariable))
        return 
$aVariable[1];
        
    return 
'';


After a quick test here are my results based on calling the function with the single argument "szString".

Calling with "szString" over 100,000 runs:
Wildhoney - 1.3253 seconds
Salathe - 0.9695 seconds

Then again, what real difference is 0.00000356 seconds per function call going to make? :p

As far as being, "under the impression that you can't beat regular loops in terms of speed," that is a good position to hold as a general rule. Sometimes looping through every character (or every item in an array, or whatever) is the clearest, simplest and fastest way to get things done. It is quite easy to get the RegEx parser bogged down - with great power comes great responsibility (to keep things moving slickly).

Finally, it would be slightly more efficient (~0.1141s faster over 100,000 runs) for Wildhoney's function to forgo the str_split and loop through the characters in the string directly if you didn't want to go the RegEx route.

Karl 09-07-2007 12:23 PM

Thanks for that information Salathe, it's very interesting to know! I must admit, I honestly thought RegEx would have been slower :o

Wildhoney 09-07-2007 12:25 PM

Quote:

Originally Posted by Karl (Post 1875)
Thanks for that information Salathe, it's very interesting to know! I must admit, I honestly thought RegEx would have been slower :o

Me too, actually. Will definitely go down the RegEx path now though.

localhost 09-07-2007 03:28 PM

Wow, That's not a very big time difference... All the books I have ever read say that Regex is slower... Maybe not :p

bluesaga 09-07-2007 10:01 PM

It depends on what usage, if you have look aheads and look behinds regex will definately be slower, it just depends how you want to use it.

Wildhoney 10-24-2007 01:28 AM

Here's a question for you people which I've been pondering and musing over. What prefix would you use for an unknown variable type?

PHP Code:

$vMyVar 'Unknown'// Varies
$uMyVar 'Unknown'// Unknown 

What else is there? It really needs one that looks good - v and u don't, to me.

bluesaga 10-24-2007 09:36 AM

Doesn't that really get classified by:

PHP Code:

$mMixed "Mixed Variable, could be array, int, float etc...."


Karl 10-24-2007 10:24 AM

I agree with Bluesaga, I always use the m prefix for mixed vars.

Salathe 10-24-2007 11:58 AM

The PHP manual uses the term "mixed" to denote uncertain type. So yes, an m prefix would seem the most appropriate choice (and is the one I use).

Wildhoney 10-24-2007 12:05 PM

In that case, m it is!

Andrew 11-05-2007 04:14 AM

Thanks for the tip. I've been using it lately, and it makes my code seem much more "professional". :)

Wildhoney 11-05-2007 12:57 PM

Yea :) it does make code a lot more professional looking - and also a lot easier to read! It shows that you know what the functions return if you set data types, and so when you skim over it you can instantly deduce what's holding what.

Andrew 11-12-2007 01:35 AM

What prefix would you guys use for MySQL resources such as a query result?


All times are GMT. The time now is 09:50 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0