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

Link to comment
Share on other sites

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. ;)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.