PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 7, 2022

[FIXED] Does using method chaining in PHP cause any problems with resources or memory?

 January 07, 2022     class, methods, object, oop, php     No comments   

Issue

I'm talking about methods like this:

    $object->method()->method1('param')->method2('param');

Those are created by returning the object in the function.

    return $this;

I've seen third-party software use that method, but I'm wondering, wouldn't that cause a bit of a problem with the resources or memory because you're continuously returning the entire object?


Solution

You are not returning the entire object, but rather a reference to the object -- that is, just the memory location where it resides. So objects aren't constantly being copied around in memory when methods are called along the chain.

By default (mainly, but read the link for actual details), objects in PHP are passed, returned, and assigned by reference.

See the PHP docs on references.



Answered By - Michael Berkowski
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing