Reply to Re: array and value type

Your name:

Reply:


Posted by Jon Slaughter on 05/12/07 21:49

"ZeldorBlat" <zeldorblat@gmail.com> wrote in message
news:1179005481.085906.112850@y80g2000hsf.googlegroups.com...
> On May 12, 3:54 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com> 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.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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация