HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

PHP(?) question

I guess it is a general PHP question, but it might as well be Vanilla related: why is there sometimes $UserModel = Gdn::UserModel(); and on other places $UserModel = new UserModel(); used? Is one of them to be preferred? Should I avoid one of both syntaxes do they differ in what is going on behind the scenes?

Best Answers

  • hgtonighthgtonight MVP
    edited March 2016 Answer ✓

    $UserModel = Gdn::UserModel() is calling a static method of the Gdn class. It always returns the same instance of the UserModel. $UserModel = new UserModel() is calling the constructor of the UserModel class with no parameters and returning the instance generated.

    The first method just gives you the (probably) already existing copy of the model while the second creates a completely separate copy.

    If you need to modify the existing model, use the static call. If you are needing a separate object, you will want a new copy.

    EDIT - crosspost

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

Answers

  • hgtonighthgtonight MVP
    edited March 2016 Answer ✓

    $UserModel = Gdn::UserModel() is calling a static method of the Gdn class. It always returns the same instance of the UserModel. $UserModel = new UserModel() is calling the constructor of the UserModel class with no parameters and returning the instance generated.

    The first method just gives you the (probably) already existing copy of the model while the second creates a completely separate copy.

    If you need to modify the existing model, use the static call. If you are needing a separate object, you will want a new copy.

    EDIT - crosspost

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • R_JR_J Admin

    Oh, that's really a huge difference! Thanks for your answers, very informative!

  • rbrahmsonrbrahmson ✭✭✭

    This is very informative @hgtonight , thanks!

    Makes me wonder whether @R_J 's response to my question yesterday (How to retrieve the currently defined column names in a garden table) that "that if you work with Gdn::sql() you work with an existing instance of the sql class, so you might have "relicts" from a former sql call or even worse, you destroy the sql that should be built before/after your custom method is called" is an example of such case and that to have a separate sql access thread one need to create a new sqlmodel instance... I'm not sure how to though.

    Hope I don't confuse unrelated concepts...

  • Yes, you are using the same object twice. You shouldn't need to use the SQLDriver object unless you are writing a model (or extending it).

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • rbrahmsonrbrahmson ✭✭✭

    Thanks! I think I am starting to "get" Vanilla;-)

Sign In or Register to comment.