Introduction: This is a detailed page for PHP virtual proxy to implement delayed loading. It introduces PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 337757 'rolling = 'no'>
This is learned from Martin's enterprise application architecture model, which assists PHP with the dynamic language features, it is much easier to implement delayed loading than Java-by using a virtual proxy placeholder. The only defect is that only proxy objects are allowed, and basic types cannot be built into the proxy.
In the PHP domain model design I tried to use this to implement delayed loading of domainobject.
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 /**
13 * virtual proxy. The closure function is called only when the accessed member is used to generate the target object.
14 *
15 * @ Param closure $ loader generates the closure function of the proxy object
16 */
17 public function _ construct (Closure $ loader)
18 {
19 $ this-> loader = $ loader;
20}
21
22 /**
23 * Call of proxy member Methods
24 *
25 * @ Param string $ Method
26 * @ Param array $ arguments
27 * @ throws badmethodcallexception
28 * @ return mixed
29 */
30 public function _ call ($ method, array $ arguments = NULL)
31 {
32 $ this-> check ();
33
34 if (! Method_exists ($ this-> holder, $ method )){
35 throw new badmethodcallexception ();
36}
37
38 return call_user_func_array (
39 array (& $ this-> holder, $ method ),
40 $ arguments );
41}
42
43 /**
44 * read proxy member attributes
45 *
46 * @ Param string $ Property
47 * @ throws errorexception
48 * @ return mixed
49 */
50 public function _ Get ($ Property)
51 {
52 $ this-> check ();
53
54 if (! Isset ($ this-> holder-> $ property )){
55 throw new errorexception ();
56}
57
58 return $ this-> holder-> $ property;
59}
60
61 /**
62 * assignment of proxy member attributes
63 *
64 * @ Param string $ Property
65 * @ Param mixed $ Value
66 */
67 public function _ set ($ property, $ value)
68 {
69 $ this-> check ();
70
71 $ this-> holder-> $ property = $ value;
72}
73
74 /**
75 * check whether a proxy object exists. If no proxy object exists, it is generated.
76 */
77 private function check ()
78 {
79 if (null ==$ this-> holder ){
80 $ loader = $ this-> loader;
81 $ this-> holder = $ loader ();
82}
83}
84}
85
86
87 // Test
88 $ v = new virtualproxy (function (){
89 echo 'Now, loading', "\ n ";
90 $ A = new arrayobject (range (1,100 ));
91 $ A-> ABC = 'a ';
92 // in actual use, the findxxx method of datamapper is called here
93 // The returned result is a collection of domain objects.
94 return $;
95 });
96 // The proxy object is directly accessed as the original object
97 // at this time, the callback function passed in by the constructor is called.
98 // delay in object Loading
99 echo $ V-> ABC. $ V-> offsetget (50 );
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/337757.html pageno: 9.