Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product

PHP(?) question

R_JR_J Cheerleader & TroubleshooterMunich Moderator

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?

Comments

  • hgtonighthgtonight ∞ · New Moderator
    edited March 2016

    $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.

    ShadowdarejackmaessenR_J
  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

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

  • rbrahmsonrbrahmson "You may say I'm a dreamer / But I'm not the only one" NY ✭✭✭

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

  • hgtonighthgtonight ∞ · New Moderator

    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.

    rbrahmson
  • rbrahmsonrbrahmson "You may say I'm a dreamer / But I'm not the only one" NY ✭✭✭

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

    hgtonight
Sign In or Register to comment.