By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,347 Members | 1,390 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,347 IT Pros & Developers. It's quick & easy.

array and value type

P: n/a
I want to create function that takes either an array or a simple type but I
want it not to distinguish between them,
function Decrypt(&$str)
{
foreach($str as &$val)
{
$val = rtrim($val,'enc');
}
return $str;
} //

So here Decrypt can be called with an array or any type but the code should
be the same

i.e., I'd like to be able to call the function like Decrypt(1) or
Decrypt('1234') and Decrypt(array('13', 4)) without having to modify the
code. Essentially I need some polymorphic behavior but I'm not sure if php
supports anything like this? What I want is that in the above foreach loop
that if str is not an array then val = str instead of it ignoring the loop
completely. I know I can get around this by type comparision but I want to
keep my decoding part(which is simply attaching enc here but is actually
more complicated) in one spot... and I'd rather not create a seperate
function if possible.

My guess is that it won't be that easy though ;/

Thanks,
Jon

May 12 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
BTW, essentially what I'm doing now is
//--------------------------
// Decrypts data
function Decrypt($str)
{
if (gettype($str) == 'array')
{
foreach($str as $val)
{
$val = rtrim($val,'enc');
}
} else
{
$str = rtrim($val,'enc');
}

return $str;
} //

the rtrims will in general be a function call and refer to the same exact
function.
May 12 '07 #2

P: n/a
On May 12, 3:54 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:
I want to create function that takes either an array or a simple type but I
want it not to distinguish between them,

function Decrypt(&$str)
{
foreach($str as &$val)
{
$val = rtrim($val,'enc');
}
return $str;

} //

So here Decrypt can be called with an array or any type but the code should
be the same

i.e., I'd like to be able to call the function like Decrypt(1) or
Decrypt('1234') and Decrypt(array('13', 4)) without having to modify the
code. Essentially I need some polymorphic behavior but I'm not sure if php
supports anything like this? What I want is that in the above foreach loop
that if str is not an array then val = str instead of it ignoring the loop
completely. I know I can get around this by type comparision but I want to
keep my decoding part(which is simply attaching enc here but is actually
more complicated) in one spot... and I'd rather not create a seperate
function if possible.

My guess is that it won't be that easy though ;/

Thanks,
Jon
This is what I usually do:

function Decrypt($str) {
if(!is_array($str))
$str = array($str);

foreach($str as &$val)
$val = rtrim($val,'enc');

return $str;
}

BTW, don't pass the parameters by reference (i.e. use & on the
parameters) unless you have a specific reason for doing so. PHP is
smart enough to do the copy just in time (i.e. when the value is
actually changed). Put another way, if you don't change the value, no
copy is made. In practice, passing parameters by reference will (in
most cases) actually hurt performance.

May 12 '07 #3

P: n/a

"ZeldorBlat" <ze********@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
On May 12, 3:54 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:
>I want to create function that takes either an array or a simple type but
I
want it not to distinguish between them,

function Decrypt(&$str)
{
foreach($str as &$val)
{
$val = rtrim($val,'enc');
}
return $str;

} //

So here Decrypt can be called with an array or any type but the code
should
be the same

i.e., I'd like to be able to call the function like Decrypt(1) or
Decrypt('1234') and Decrypt(array('13', 4)) without having to modify the
code. Essentially I need some polymorphic behavior but I'm not sure if
php
supports anything like this? What I want is that in the above foreach
loop
that if str is not an array then val = str instead of it ignoring the
loop
completely. I know I can get around this by type comparision but I want
to
keep my decoding part(which is simply attaching enc here but is actually
more complicated) in one spot... and I'd rather not create a seperate
function if possible.

My guess is that it won't be that easy though ;/

Thanks,
Jon

This is what I usually do:

function Decrypt($str) {
if(!is_array($str))
$str = array($str);

foreach($str as &$val)
$val = rtrim($val,'enc');

return $str;
}
yeah, I was thinking about somethign like that but didn't know how to go
about it and didn't want to return an array... but I guess I can convert it
to an array, do the code, then convert it back.
BTW, don't pass the parameters by reference (i.e. use & on the
parameters) unless you have a specific reason for doing so. PHP is
smart enough to do the copy just in time (i.e. when the value is
actually changed). Put another way, if you don't change the value, no
copy is made. In practice, passing parameters by reference will (in
most cases) actually hurt performance.
I took it off because I wanted to return the results and not modify them but
in this case I used it because it would be faster since I was going to
change every element in the array. (essentially converting from the
encrypted for to decrypted form).

It it was a large array to convert then it would hurt performance to first
copy everything and then decrypt. But since I already had the rest of my
code setup to use return values and chances are the arrays will be small I
just used copies instead of references.

Thanks,
Jon
May 12 '07 #4

P: n/a
Jon Slaughter wrote:
I want to create function that takes either an array or a simple type but I
want it not to distinguish between them,
function Decrypt(&$str)
{
foreach($str as &$val)
{
$val = rtrim($val,'enc');
}
return $str;
} //

So here Decrypt can be called with an array or any type but the code should
be the same
I have the following function in my 'standard library'

function ary($a = NULL) {
return is_null($a) ? array() : (is_array($a) ? $a : func_get_args());
}

using this, your 'Decrypt' and other 'polymorphic' functions can be
simplified to

function polyfunc($arg) {
foreach(ary($arg) as $val)
.....

}
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 13 '07 #5

P: n/a

"gosha bine" <st********@gmail.comwrote in message
news:46**********************@read.cnntp.org...
Jon Slaughter wrote:
>I want to create function that takes either an array or a simple type but
I want it not to distinguish between them,
function Decrypt(&$str)
{
foreach($str as &$val)
{
$val = rtrim($val,'enc');
}
return $str;
} //

So here Decrypt can be called with an array or any type but the code
should be the same

I have the following function in my 'standard library'

function ary($a = NULL) {
return is_null($a) ? array() : (is_array($a) ? $a : func_get_args());
}

using this, your 'Decrypt' and other 'polymorphic' functions can be
simplified to

function polyfunc($arg) {
foreach(ary($arg) as $val)
.....

}
Cool that will work just fine.

Thanks, Jon
May 13 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.