Added: 1 year ago
From: JREAMdesign
Views: 3,017
Sort by time | Sort by thread (beta)

Link to this comment:

Share to:

All Comments (14)

Sign In or Sign Up now to post a comment!
  • 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 Oh I see, sorry I didnt totally understand what you were asking lol

  • 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

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

    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?

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

  • good job

  • what is the difference between aggregation and composition?

    im kinda confuse

  • @slier81 Aggregation is grouping many things together (I dont know of an example in PHP, but in SQL a GROUP BY is aggregating rows). Composition has to do with where and how its created.

  • @JREAMdesign

    i thought aggregation like importing another object into some class

    and compistion mean some class instantiate some object directly inside the class

    so from what i understand ,that mean if u aggregate an object, that object can be used by more than one class but in composition only a class that instantiate that object can use that object and if the class is out of scope so do the object..hmm maybe i just confuse...sigh ood really annoy me

  • @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

  • In a realistic example, you may want to make Action a singleton, or instantiate $this->action inside the Player construct.

Loading...
Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more