Jump to content

Private class function/property repeater please!


rwwd

Recommended Posts

Hi there people of the PHP forum,

 

This functions intention is to output the same code 4 digit code twice before generating a new one, I can get it to do one fine, but I cannot for the life of me figure out how to get it to repeat twice before changing on the third call.

 

private function KeyGen(){

$Numerical 	= range(0,9);
$Alpha		= range("A","Z");

$Keys = array_merge($Numerical, $Alpha);
return $Keys[rand(0,35)].$Keys[rand(0,35)].$Keys[rand(0,35)].$Keys[rand(0,35)];
}

 

I am needing it to do this so that each 'set' of 2 calls to the function (within the class) gives the same 4 digit code.  At the moment I am just making one call & then assigning that single call to a variable and then referencing that var twice, this is not what I want to do, but it scratches the itch for now...

 

Any idea's/other logic would be greatly appreciated.

 

Cheers,

Rw

While agreeing with thorpe, I guess what you could do is pass the second pin as a function property and if it's set it just repeats whatever you passed previously.

 

function KeyGen($pin = ''){

$Numerical = range(0,9);
$Alpha = range("A","Z");

$Keys = array_merge($Numerical, $Alpha);

$Gen = $Keys[rand(0,35)].$Keys[rand(0,35)].$Keys[rand(0,35)].$Keys[rand(0,35)];

if ($pin != '') {
	return $pin;	
}
else {
	return $Gen;
}
}

$PinNum = KeyGen(); //First call.
$SecondPin = KeyGen($PinNum); //Second Call.

echo $PinNum . ' - ' . $SecondPin; //Outputs the same thing twice.

 

Also, I love your signature. You forgot Harp, though. ;)

I guess what you could do is pass the second pin as a function property and if it's set it just repeats whatever you passed previously.

 

Because this is a method within an object and object have state, it would be easy enough to use a variables to remember how many times this method has been called and what it returned previously. Like I said though, I think its a bad decision.

Hi there Thorpe,

 

Could you elaborate a little bit please? So far as I am concerned, this function just creates a four (maybe six) digit code to append to the end of some data, I was previously using shuffle() as well as the rand() but just using rand is less intensive on the parser, then that data is used to form a 'secure' cookie for when a user logs in, and as there are two separate calls to this function I need the third to be the start of the next set; so in essence two sets of two...

 

I'm seriously considering rewriting the way as I do this because I seem to have wandered into a dead end; but the optimist in me says "carry on, the solution is just ahead".

 

@pornophobic Cheers for the input, I can see the logic there, but the calls to the function should be separate calls not calls to itself, though I do like that Idea.

 

As for the signature, I saw something similar a while ago but using food, so my next natural thought was beer!

 

Cheers,

Rw

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.