Virtual Proxy for delayed loading

Source: Internet
Author: User
Virtual Proxy for delayed loading

This product was learned from Martin's "Enterprise Application Architecture Model", which assists PHP with the dynamic language features and can implement LazyLoad much easier than Java ). The basic principle is to use a Virtual Proxy as a placeholder. once a member (method or attribute) of the Proxy object is accessed, the loading is triggered.

However, the version I implemented has limitations:

  1. It is applicable only to objects and cannot represent arrays or other basic data types (it must be encapsulated with built-in objects such as ArrayObject)
  2. After being proxies, some interface implementations with operator overload properties will become invalid, such as the ArrayAccess indexer and Itreator iterator. if this proxy is used to handle delayed loading of collection types, you also need to inherit a subclass for special processing so that the foreach iteration can be used externally.

For more information, see my blog: http://tonyseek.tumblr.com/post/6166066775/virtual-proxy-lazy-load.

  1. // Test
  2. $ V = new VirtualProxy (function (){
  3. Echo 'now, loading', "\ n ";
  4. $ A = new ArrayObject (range (1,100 ));
  5. $ A-> abc = 'a ';
  6. // In actual use, the findXXX method of DataMapper is called here
  7. // The returned result is a collection of domain objects.
  8. Return $;
  9. });
  10. // The proxy object is directly accessed as the original object
  11. // At this time, the callback function passed in by the constructor is called.
  12. // Delay in object loading
  13. Echo $ v-> abc. $ v-> offsetGet (50 );

  1. /**
  2. * Virtual proxy: The closure function is called only when the Accessed member is used to generate the target object.
  3. *
  4. * @ Author tonyseek
  5. *
  6. */
  7. Class VirtualProxy
  8. {
  9. Private $ holder = null;
  10. Private $ loader = null;
  11. /**
  12. * Virtual proxy: The closure function is called only when the Accessed member is used to generate the target object.
  13. *
  14. * @ Param Closure $ loader generates the Closure function of the proxy object
  15. */
  16. Public function _ construct (Closure $ loader)
  17. {
  18. $ This-> loader = $ loader;
  19. }
  20. /**
  21. * Call of proxy member methods
  22. *
  23. * @ Param string $ method
  24. * @ Param array $ arguments
  25. * @ Throws BadMethodCallException
  26. * @ Return mixed
  27. */
  28. Public function _ call ($ method, array $ arguments = null)
  29. {
  30. $ This-> check ();
  31. If (! Method_exists ($ this-> holder, $ method )){
  32. Throw new BadMethodCallException ();
  33. }
  34. Return call_user_func_array (
  35. Array (& $ this-> holder, $ method ),
  36. $ Arguments );
  37. }
  38. /**
  39. * Read proxy member attributes
  40. *
  41. * @ Param string $ property
  42. * @ Throws ErrorException
  43. * @ Return mixed
  44. */
  45. Public function _ get ($ property)
  46. {
  47. $ This-> check ();
  48. If (! Isset ($ this-> holder-> $ property )){
  49. Throw new ErrorException ();
  50. }
  51. Return $ this-> holder-> $ property;
  52. }
  53. /**
  54. * Value assignment of proxy member attributes
  55. *
  56. * @ Param string $ property
  57. * @ Param mixed $ value
  58. */
  59. Public function _ set ($ property, $ value)
  60. {
  61. $ This-> check ();
  62. $ This-> holder-> $ property = $ value;
  63. }
  64. /**
  65. * Check whether a proxy object already exists. If no proxy object exists, it is generated.
  66. */
  67. Private function check ()
  68. {
  69. If (null ==$ this-> holder ){
  70. $ Loader = $ this-> loader;
  71. $ This-> holder = $ loader ();
  72. }
  73. }
  74. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.