RE: [PHP] 'Code Snippets' you couldn't live without

2004-11-04 Thread Murray @ PlanetThoughtful
> Me too (tm)!  This is a job for a good editor, with automatic name
> completion or abbreviation expansion.  This gives you the convenience of
> shortcuts when coding, together with the full standard names when reading
> the source code at a later date!

I think we've missed the point of my original question. Not that a
discussion of good coding principles isn't welcome, but at the same time,
there can be valid (if subjective) reasons for using shorthand functions in
the way I mentioned in my original post.

My valid reason is that I have RSI. To help alleviate that, I have a small
handful of functions I commonly use many times over defined in a way that
means less keystrokes. Across the scope of a project, that can mean
significantly less typing, and marginally less discomfort. My hands thank me
every time I use these shorthand functions.

That having been said, and purist principles aside for the moment, I doubt
there's an intermediate to advanced coder on this list who isn't comfortable
with exploring include files to find out what a function does or how a class
operates. I doubt half-a-dozen shorthand functions in that include file
would place a measurable strain on the readability or maintainability of a
project. 

Yes, I'm sure we can all generate extreme examples of code rendered utterly
turgid and obscure by random and wholesale shorthand transformation. But
let's be honest, you can make any suggestion or process seem ridiculous by
rendering it in the extreme.

The core PHP language is not without it's own shorthand approaches. Witness
the ternary operator. At a glance, it's probably not obvious what it's doing
when first viewed by someone new to PHP. Also witness the deprecation of
variables such as $HTTP_GET_VARS in favour of $_GET etc. Or the .=
concatenation assignment operator as a shorthand to $var = $var . ' added
string.' Or the echo shorthand of .

My point being, the most purist coder is almost certainly using shorthand
methods built into the language. To suggest that these shorthand methods
should never be extended, when approached sensibly and for good reasons,
seems not only regressive, but also somewhat blind to the reality of the
language itself.

Of course, I realize this is a difficult position to defend. I understand
that, in theory, using shorthand functions in this way is 'wrong.' But,
after all, life is about compromise. Sometimes we get to choose the
compromises we make, sometimes we don't. More power to those who struggle
against compromise in this issue. You are a beacon of sanity and reason to
us all.

Back to the intent rather than the form of my original question: what are
the code snippets you can't live without?

Much warmth,

Murray
http://www.planetthoughtful.org
Building a thoughtful planet,
One quirky comment at a time.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] 'Code Snippets' you couldn't live without

2004-11-04 Thread Ford, Mike
To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm



On 03 November 2004 19:40, Pablo Gosse wrote:

> [snip]Klaus Reimer wrote:
> > Murray @ PlanetThoughtful wrote:
> > > function asl($val){
> > > function mfr(&$rset){
> > 
> > This may be ok for private projects but otherwise I don't think
> > it's a good idea to create "shorthand" functions. Other developers
> > (and especially new developers beginning to work on the project) are
> > getting confused. They must first learn what this functions are
> > doing. The names of these functions are not really helpful in
> > understanding what's going on.[/snip]
> 
> I'm with Klaus on this one.

Me too (tm)!  This is a job for a good editor, with automatic name
completion or abbreviation expansion.  This gives you the convenience of
shortcuts when coding, together with the full standard names when reading
the source code at a later date!

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] 'Code Snippets' you couldn't live without

2004-11-04 Thread Murray @ PlanetThoughtful
Another code snippet I use quite regularly builds a form select list from a
delimited string.

function displayFormSelect($selvals, $selname, $val=''){
$optval = explode(",", $selvals);
$retval = "";
foreach ($optval as $key => $value){
$retval .= "";

}
$retval .= "";
return $retval; 
}

Basically, I use it like so:

$optstring = 'Option 1,Option 2,Option 3';
$optname = 'frmoptions';
$optsel = 'Option 3';

$formsel = displayFormSelect($optstring, $optname, $optse);

echo $formsel;

The optional third value defines the option that appears 'selected' if
provided.

Looking at it, it would be pretty easy to change this to pass a recordset
array to it etc.

Much warmth,

Murray
http://www.planetthoughtful.org
Building a thoughtful planet,
One quirky comment at a time.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] 'Code Snippets' you couldn't live without

2004-11-03 Thread Klaus Reimer
Greg Donald wrote:
Murray wasn't asking for your opinions on _his_ code, he was asking
what code _you_ had that you couldn't live without, code that makes
your life easier when reused from project to project.
Ok then. Here comes mine (but not in form of source code. If someone is 
interested, mail me).

I'm doing a lot with dynamic image generation and so I've written a 
gdutils include file containing the following:

imagecolorallocatehex($image, $color) - Allocate a color for an image 
from hexadecimal value. Useful to have the same color syntax for HTML 
and GD output.

imagecolorallocatehexalpha($image, $color, $alpha) - Pretty the same as 
the first function but this time for alpha transparent colors.

imagecreatefromfile($filename) - Checks the image format with 
getimagesize() and loads the image with one of the 
imagecreatefrom(gif|jpeg|png|bmp|xbm) functions.

img_type_to_extension($imgtype, $include_dot = false) - Returns the 
extension for the given image type.

imagettfbbox2($size, $angle, $fontfile, $text, $fix_vertical = false) - 
An improved version of imagettfbbox. Returns "correct" data (even if 
text is rotated) and also returns "more useful" data (associative array 
with keys left, right, top, bottom, width and height.

imagecopyresampled2($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $dstW, 
$dstH, $srcW, $srcH) - A workaround for the buggy imagecopyresampled. 
The GD library can't really resample images with alphatransparency (see 
bug #29315). So my function passes the GD image to 
ImageMagick/GraphicsMagick (with the imagick module from PECL) which 
resamples the image and passes it back to GD.

And two more functions I'm using regularly:
mkpath($path, $mode) - Create a directory with all parent directories 
and make sure the directory mode is really set (setting umask to 0 
during directory creation)

redirect($url) - Creates a correct url (absolute and with SID if 
needed), sends location header and exits the script.

--
Bye, K  (FidoNet: 2:240/2188.18)
[A735 47EC D87B 1F15 C1E9  53D3 AA03 6173 A723 E391]
(Finger [EMAIL PROTECTED] to get public key)


signature.asc
Description: OpenPGP digital signature


RE: [PHP] 'Code Snippets' you couldn't live without

2004-11-03 Thread Jason Davis
this is not a code "snippet"  just a class that i have re-used about
a 100 times. It is for authenticating users via a passwd file or mysql.

Hope it will help someone ... also , if you improve it please email me
the changes :)

http://mohadib.openactive.org/web_editor/AuthTool.class.php.txt


jd

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] 'Code Snippets' you couldn't live without

2004-11-03 Thread Pablo Gosse
[snip]Extending PHP to have "shorthand" functions? Was that
irony?[/snip]

It most certainly was irony, Klaus.

I was merely trying to illustrate the point that writing a custom
function which does EXACTLY the same thing as a built in function is
redundant.  A number of functions within php do have aliases (sizeof()
is an alias count() for example) but these are throwbacks to equitable
functions in other languages.

As I stated earlier, if your function does more than the built in
function, then yes, you are increasing efficiency, but if all you're
doing is creating a custom alias of which only you will know the useage,
then you only increase obscurity.

As for my favorite piece of reusable code, I hate preparing dates for
insertion into a database when I have to deal with null values, etc.  So
I use a function which takes an array of date field names and a regular
expression, then validates $_POST['field name'] against the regular
expression and returns an array, $dates, each field of which contains
either a date prepared for insertion (wrapped in single quotes) or
simply the string 'null'.

function _formatPostDates($dateArr,$regex)
{
$dates = array();
foreach ($dateArr as $date) {
$dates[$date] = preg_match($regex,$_POST[$date]) ?
"'{$_POST['release']}'" : "null";
}
return $dates;
}

On a page where I have to deal with multiple date fields, this greatly
increases the efficiency of validating the dates and makes things safer
and easier when creating queries.

Cheers,
Pablo

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] 'Code Snippets' you couldn't live without

2004-11-03 Thread Gryffyn, Trevor
I agree that shorthand is probably bad for, BUT... Greg is absolutely
correct.  The gist of the post was "What do YOU use frequently".  His
examples were good enough to illustrate the idea.

For me, I have a handful of includes that are in most/a lot of my
scripts.   I'm not going to paste them here because they're rather
lengthy (which is why I have them as includes).

They include the following:

1. ADOdb + a dbconnect include.  I like using ADOdb because for a while
I was using MySQL, Oracle AND SQL Server (now mostly just SQL Server,
but some Oracle as well).  Even though I don't really use ADOdb's full
power, I like being able to write standard SQL and use a few prep
variables and connect to whereever I need to.   I have my connection
strings defined in my 'dbconnect' and just say $database = "Oracle".
Pass the database, the sql string and I also send the report name (I use
the "APP=$reportname" in my connection string so I can identify one of
my scripts' processes looking at the process list on the database
server).   The results come back in an array that I use the same set of
commands to parse when it's done.My "dbconnect" also logs db errors
to a text file or alternately logs ALL sql queries (if I set a
$verbosity variable).

2. dateconvert - I have a handful of functions to convert from one date
format to another.  This is helpful in so many ways.  Nuff said

3. metrics - Logging script usage by snagging the NT username, user's IP
address, time, date, script run, etc



Those are the big ones.


This kind of topic is kind of useful for the PHP development team as
well because if there are functions that people keep writing themselves,
then they may be good candidates to make into standard functions.   I'm
always amazed at how much PHP does already and I'm guessing a lot of
those functions came out of someone saying "Hey..  We all use this type
of function CONSTANTLY..  Can it be built into PHP's core instead of us
having to INCLUDE it all the time?"

-TG

> -Original Message-
> From: Greg Donald [mailto:[EMAIL PROTECTED] 
> Sent: Wednesday, November 03, 2004 2:56 PM
> To: [EMAIL PROTECTED]
> Subject: Re: [PHP] 'Code Snippets' you couldn't live without
> 
> 
> On Wed, 03 Nov 2004 20:22:14 +0100, Klaus Reimer <[EMAIL PROTECTED]> wrote:
> > This may be ok for private projects but otherwise I don't 
> think it's a
> > good idea to create "shorthand" functions.
> 
> Murray wasn't asking for your opinions on _his_ code, he was asking
> what code _you_ had that you couldn't live without, code that makes
> your life easier when reused from project to project.
> 
> Criticism comes easy when you have nothing to contribute otherwise.
> 
> 
> -- 
> Greg Donald
> Zend Certified Engineer
> http://gdconsultants.com/
> http://destiney.com/
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] 'Code Snippets' you couldn't live without

2004-11-03 Thread Klaus Reimer
Pablo Gosse wrote:
extra with the results to suit your needs, but as for just using
asl($val) instead of addslashes($val), well why not just extend the PHP
source to make asl() an actual alias to addslashes()?
Extending PHP to have "shorthand" functions? Was that irony? I hope so. 
Having such shorthand functions directly in PHP might bring us this:

$n = $_I['n'];
$c = mc('localhost');
$d = msd('somedb', $c);
$q = mq(spf('SELECT * FROM users WHERE name='%s', asl($n)), $d);
while ($r = mfa($q)) vd($r);
mfr(q);
Is that really what you want?
--
Bye, K  (FidoNet: 2:240/2188.18)
[A735 47EC D87B 1F15 C1E9  53D3 AA03 6173 A723 E391]
(Finger [EMAIL PROTECTED] to get public key)


signature.asc
Description: OpenPGP digital signature


Re: [PHP] 'Code Snippets' you couldn't live without

2004-11-03 Thread Alex Hogan
> snippets they couldn't live without when working on a PHP project. IE, the
> kind of re-usable bits and pieces that make your life a lot easier.

I hate always having to write queries for inserting and updating a db.
 With this I can just name the form objects the same names as the
table fields and I'm done.

function insert($table, $fld, $val){
   $query = "INSERT INTO $table (%s) VALUES (%s)";
   $query = sprintf($query, implode(",", $fld), implode(",", $val));
   $result = mssql_query($query) or die;
}   


alex hogan

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] 'Code Snippets' you couldn't live without

2004-11-03 Thread phpninja
I would have to say I could not live without pear.

http://pear.php.net

It is quick and clean, ive reused it in about 3-4 projects easily. Its
built into php when you download php. just run the go-pear batch file
in the root directory, and it will set it up easily. From there you
can get real organized with your code. Try to play around with it if
you havent looked at it yet, its great. Not exactly 'code snippits' in
the sence of reusable functions, but great for reuse project to
project..

phpninja

-Original Message-
From: Murray @ PlanetThoughtful [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, November 03, 2004 10:17 AM
To: [EMAIL PROTECTED]
Subject: [PHP] 'Code Snippets' you couldn't live without

Hi All,

I'm curious to know what other members of the list think of as the code
snippets they couldn't live without when working on a PHP project. IE, the
kind of re-usable bits and pieces that make your life a lot easier.

For myself, I have an include file in which I define 'shorthand' functions
for functions etc I use on a regular basis.

Eg:

function asl($val){
return addslashes($val);
}

function mfr(&$rset){
mysql_free_result($rset);
}

...and so on.

Just thought it would be interesting to see what others do to make
programming more bearable.

Much warmth,

Murray
http://www.planetthoughtful.org
Building a thoughtful planet,
One quirky comment at a time.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] 'Code Snippets' you couldn't live without

2004-11-03 Thread John Nichel
Murray @ PlanetThoughtful wrote:
Hi All,
I'm curious to know what other members of the list think of as the code
snippets they couldn't live without when working on a PHP project. IE, the
kind of re-usable bits and pieces that make your life a lot easier.
For myself, I have an include file in which I define 'shorthand' functions
for functions etc I use on a regular basis.
Eg:
function asl($val){
return addslashes($val);
}
function mfr(&$rset){
mysql_free_result($rset);
}
...and so on.
Just thought it would be interesting to see what others do to make
programming more bearable.
Probably the custom one I use the most is for getting data...
function selectData ( $sql ) {
if ( ! $_SESSION['mysql']['status'] ) {
dbconn();
}
if ( $result = @mysql_query ( $sql ) ) {
$return = array();
while ( $row = @mysql_fetch_array ( $result ) ) {
array_push ( $return, $row );
}
@mysql_free_result ( $result );
return $return;
} else {
$msg = "The query could not be executed.";
raiseError ( $msg, 0, 1 );
return false;
}
}
--
John C. Nichel
ÜberGeek
KegWorks.com
716.856.9675
[EMAIL PROTECTED]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] 'Code Snippets' you couldn't live without

2004-11-03 Thread Greg Donald
On Wed, 03 Nov 2004 20:22:14 +0100, Klaus Reimer <[EMAIL PROTECTED]> wrote:
> This may be ok for private projects but otherwise I don't think it's a
> good idea to create "shorthand" functions.

Murray wasn't asking for your opinions on _his_ code, he was asking
what code _you_ had that you couldn't live without, code that makes
your life easier when reused from project to project.

Criticism comes easy when you have nothing to contribute otherwise.


-- 
Greg Donald
Zend Certified Engineer
http://gdconsultants.com/
http://destiney.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] 'Code Snippets' you couldn't live without

2004-11-03 Thread Pablo Gosse
[snip]Klaus Reimer wrote:
> Murray @ PlanetThoughtful wrote:
>> function asl($val){
>> function mfr(&$rset){
> 
> This may be ok for private projects but otherwise I don't think it's a
> good idea to create "shorthand" functions. Other developers (and
> especially new developers beginning to work on the project) are
> getting confused. They must first learn what this functions are
> doing. The names of these functions are not really helpful in
> understanding what's going on.[/snip]

I'm with Klaus on this one.

I see the purpose of creating functions that eliminate repetitive
behaviour by using native PHP functions and doing a little something
extra with the results to suit your needs, but as for just using
asl($val) instead of addslashes($val), well why not just extend the PHP
source to make asl() an actual alias to addslashes()?

My $0.02 (CDN).

Pablo

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] 'Code Snippets' you couldn't live without

2004-11-03 Thread Klaus Reimer
Murray @ PlanetThoughtful wrote:
function asl($val){
function mfr(&$rset){
This may be ok for private projects but otherwise I don't think it's a 
good idea to create "shorthand" functions. Other developers (and 
especially new developers beginning to work on the project) are getting 
confused. They must first learn what this functions are doing. The names 
of these functions are not really helpful in understanding what's going on.

--
Bye, K  (FidoNet: 2:240/2188.18)
[A735 47EC D87B 1F15 C1E9  53D3 AA03 6173 A723 E391]
(Finger [EMAIL PROTECTED] to get public key)


signature.asc
Description: OpenPGP digital signature


Re: [PHP] 'Code Snippets' you couldn't live without

2004-11-03 Thread Greg Donald
On Thu, 4 Nov 2004 04:16:48 +1000, Murray @ PlanetThoughtful
<[EMAIL PROTECTED]> wrote:
> I'm curious to know what other members of the list think of as the code
> snippets they couldn't live without when working on a PHP project. IE, the
> kind of re-usable bits and pieces that make your life a lot easier.

function slashes( $var )
{
if ( is_array( $var ) )
return array_map( 'slashes', $var );
else
return addslashes( $var );
}

set_magic_quotes_runtime( 0 );
if ( get_magic_quotes_gpc() == 0 )
{
$_GET = isset( $_GET ) ? array_map( 'slashes', $_GET ) : array();
$_POST  = isset( $_POST ) ? array_map( 'slashes', $_POST ) : array();
$_COOKIE = isset( $_COOKIE ) ? array_map( 'slashes', $_COOKIE ) : array();
}


-- 
Greg Donald
Zend Certified Engineer
http://gdconsultants.com/
http://destiney.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php