Reply to Re: array and value type

Your name:

Reply:


Posted by ZeldorBlat on 05/12/07 21:31

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;
}

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.

[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

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