Virtual agents implement lazy loading

Source: Internet
Author: User

The goods are learned from the "Enterprise Application Architecture Model" of Martin great God, and the features of the auxiliary PHP Dynamic language can be deferred loaded (lazyload) much more easily than Java. The rationale is that a virtual proxy is used as a placeholder, and once a member (method or property) of the proxy object is accessed, the load is triggered.

But this version of my implementation has limitations:

    1. Applies to objects only, cannot proxy arrays and other basic data types (need to be encapsulated with a arrayobject class of built-in objects)
    2. After being proxied, some interface implementations with operator overloading are invalidated, such as arrayaccess indexers, Itreator iterators, and if this proxy is used to handle deferred loading of collection types, you also need to inherit a subclass to do special processing for external foreach iterations

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 practice, this is called the Findxxx method of Datamapper.
    7. Returns a collection of domain objects
    8. return $a;
    9. });
    10. The proxy object is accessed directly as the original object
    11. The callback function passed in by the constructor method is called when the
    12. This allows for delay in loading object operations
    13. Echo $v->abc. $v->offsetget (50);
Copy Code
  1. /**
  2. * Virtual proxy, which calls the closure function to generate the target object only when the member is accessed.
  3. *
  4. * @author Tonyseek
  5. *
  6. */
  7. Class Virtualproxy
  8. {
  9. Private $holder = null;
  10. Private $loader = null;
  11. /**
  12. * Virtual proxy, which calls the closure function to generate the target object only when the member is accessed.
  13. *
  14. * @param Closure $loader generate the closure function of the Proxied object
  15. */
  16. Public function __construct (Closure $loader)
  17. {
  18. $this->loader = $loader;
  19. }
  20. /**
  21. * Invocation 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 of Agent member properties
  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. * Assignment of Proxy member properties
  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 if the proxy object already exists, and the build does not exist.
  66. */
  67. Private Function Check ()
  68. {
  69. if (null = = $this->holder) {
  70. $loader = $this->loader;
  71. $this->holder = $loader ();
  72. }
  73. }
  74. }
Copy Code
  • 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.