Uploader Comments (JREAMdesign)
All Comments (14)
-
@JREAMdesign Out of curiousity, is there a special name given to that? (That is, using {$type}() to dynamically choose what function to call).
Also, are you able to do that in any other languages?
Only, I am a fan of Java and C#, as well as PHP. So I was wondering if there is a way to do it in those languages.
If not, how might you handle this scenario otherwise?
- Thanks in advance.
-
hi there, what about if you want to pass different parameters everytime, but not having all the other info repeating, would you need to create a seperate function?
-
public function action($type, $dmg, $firstname,$lastname, $gender)
{ $action = new Action($this); try{ echo $action -> {$type}($dmg); // FROM FUNCTION ARGUMENTS IN THIS FUNCTION HOW DO YOU HANDLE ( echo $action -> {$type}($dmg); ) } catch (Exception $e) { echo $e->getMessage(); }
THE COMMENTS LINE IS WHAT I HAVE BEEN TRYING TO UNDERSTAND FROM YOUR TUTORIAL, MANY THANKS FOR YOUR HELP.
-
Hi JREAMdesign,
Thank you for your quick response, the explanation is very helpful and gives me more understanding to composition through your excellent tutorial.
However part of my questions has not been answered yet, which is , how do you handle two classes with functions that have more than two arguments passed to the functions. OR you always need two parameters as in {$type}($dmg) to create an object of a class in another class, and use the object created to call other functions? Thanks
-
good job
-
@slier81 I did a little skimming around and it appears that the key to aggregation is passing a variable by reference. By the descriptions I've read it's difficult to try and explain and follow. I have never seen this word come up in any of my books for OOP (totally serious) - But I'm making my second round in Pragmatic Programmer, it's possible I overlooked it :P
Thank you for great tutorial and response. I fiddled with three arguments and it worked, I found that you simply parse the rest as a list in the () brackets as below.
public function action($type,$name, $dmg)
{ $action = new Action($this); try{ echo $action -> {$type}($name,$dmg); } catch (Exception $e) { echo $e->getMessage(); }
}
codeRealm 1 year ago
@codeRealm Oh I see, sorry I didnt totally understand what you were asking lol
JREAMdesign 1 year ago
Hi JREAMdesign,#
Can you please explain to me this part of the code in necessar composition, It works and I grateful you put a great tutorial on here to help novices like me, however I will like to understand the code for future use and manipulation.
public function action($type, $dmg){ $action = new Action($this); try{ echo $action -> {$type}($dmg); } catch (Exception $e) {echo $e->getMessage(); } } My question is why is $type is curly brackets and not $dmg? why different brackets
codeRealm 1 year ago
@codeRealm Good Question! Because if we had: $action->$type() it will not parse the $type variable correctly into $action->player() or $action->bot(). We want $type to be read as a function -- So we do $action->{$type}() so it first parses into $action->player() or $action->bot(). Then, the $dmg is the value passed into the method as you would any other function.
JREAMdesign 1 year ago