Virtual proxy implements lazy loading

WBOY
Release: 2016-07-25 09:09:57
Original
884 people have browsed it

This thing was learned from Martin's "Enterprise Application Architecture Patterns". It assists the characteristics of PHP dynamic language and can implement lazy loading (LazyLoad) much easier than Java. The basic principle is to use a virtual proxy (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. Only applicable to objects, cannot proxy basic data types such as arrays (need to be encapsulated by built-in objects such as ArrayObject)
  2. After being proxied, some interface implementations with operator overloading properties become invalid, such as the indexer of ArrayAccess and the iterator of Itreator. If the proxy is used to handle lazy loading of collection types, a subclass needs to be inherited to do special Processed so that it can be iterated externally using foreach

See my blog for details: 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. //What is returned is a collection of domain objects
  8. return $a;
  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. // To achieve the delay of loading object operation
  13. echo $v->abc . $v->offsetGet(50);
Copy code
  1. /**
  2. * Virtual proxy, the closure function is called 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, the closure function is called to generate the target object only when the member is accessed.
  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 method
  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. * Reading of proxy 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 whether the proxy object already exists, and generate it if it 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


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!