Good vs. Evil

  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
76 going to grab something to eat and then back at working on the project
(and cursing at php for stupid design. Fun fact: in php you can use rand() or mt_rand() to get a random number, however rand() takes more time then mt_rand() and is less random......)
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
77 well rand() may be usefull in certain situation, I think
78 I am now curious in what situation you want to get a random number that isn't as random as it could be while also having it take longer to create ?
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
80 indeed, the use apparently only looks at what the value is when you define the function, not when you execute it.
PHP:
$someFunction=function() use($lol){ //the code will crash here already because $lol is not defined
echo $lol;
}
$lol="test";
$someFunction(); //even though I execute the function here
 

Someone Else 37

Forum Addict
Feb 10, 2013
1,876
1,440
168
81 It makes sense that that would crash if PHP just goes through the source file once, like C does, rather than scanning once to get the names of all the functions and global variables and then again to actually compile those functions, like Java. When you declare $someFunction in the above example, $lol doesn't exist yet, so it crashes.

What does the example on the previous page actually print? Your response confuses me.
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
84 don't mind the fact that the original post has a spoiler containing the answer

75 and I found a new fun thing about php
It will print test. This is because the use keyword only looks at the values of the variables when you declare the function and not when you actually use the function
 

Someone Else 37

Forum Addict
Feb 10, 2013
1,876
1,440
168
85 But I thought it would print test2, and then you said indeed, and I was confused.

Is there a way to make a PHP function use the current value of a global variable, as opposed to the value when the function was defined?
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
86 Must have misread then, sorry :)

Also, I am not going to go the path of globals, yes it is possible to do (but also a pain because php is weird). Instead I am just going to do it in a bit all over the place way (as that is still better then globals).
PHP:
//thanks to code igniter I am already working inside classes
class Card_interperter extends My_model{
public function doDamage(){
//code that deals damage here
}
public function executeEffect(){
$that=$this;
$dealDamage=function() use ($that){
$that->dealDamage();
}
$this->someVariableINeed="lols";
$dealDamage();
}
}
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
88 GREAT JUSTICE HAS BEEN DONE THIS DAY!
judge_jury_and_executioner__but_mostly_executioner_by_tobias_sama-d5hkucc.png