PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/kdewebdev-3.5.10/php/ref.overload.html

#
HTML | 465 lines | 456 code | 9 blank | 0 comment | 0 complexity | 55620d0fa6a9fa2a599a80c91973e521 MD5 | raw file
Possible License(s): AGPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. <HTML
  2. ><HEAD
  3. ><TITLE
  4. >Object property and method call overloading</TITLE
  5. ><META
  6. NAME="GENERATOR"
  7. CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
  8. REL="HOME"
  9. TITLE="PHP Manual"
  10. HREF="index.html"><LINK
  11. REL="UP"
  12. TITLE="Function Reference"
  13. HREF="funcref.html"><LINK
  14. REL="PREVIOUS"
  15. TITLE="ob_start"
  16. HREF="function.ob-start.html"><LINK
  17. REL="NEXT"
  18. TITLE="overload"
  19. HREF="function.overload.html"><META
  20. HTTP-EQUIV="Content-type"
  21. CONTENT="text/html; charset=ISO-8859-1"></HEAD
  22. ><BODY
  23. CLASS="reference"
  24. BGCOLOR="#FFFFFF"
  25. TEXT="#000000"
  26. LINK="#0000FF"
  27. VLINK="#840084"
  28. ALINK="#0000FF"
  29. ><DIV
  30. CLASS="NAVHEADER"
  31. ><TABLE
  32. SUMMARY="Header navigation table"
  33. WIDTH="100%"
  34. BORDER="0"
  35. CELLPADDING="0"
  36. CELLSPACING="0"
  37. ><TR
  38. ><TH
  39. COLSPAN="3"
  40. ALIGN="center"
  41. >PHP Manual</TH
  42. ></TR
  43. ><TR
  44. ><TD
  45. WIDTH="10%"
  46. ALIGN="left"
  47. VALIGN="bottom"
  48. ><A
  49. HREF="function.ob-start.html"
  50. ACCESSKEY="P"
  51. >Prev</A
  52. ></TD
  53. ><TD
  54. WIDTH="80%"
  55. ALIGN="center"
  56. VALIGN="bottom"
  57. ></TD
  58. ><TD
  59. WIDTH="10%"
  60. ALIGN="right"
  61. VALIGN="bottom"
  62. ><A
  63. HREF="function.overload.html"
  64. ACCESSKEY="N"
  65. >Next</A
  66. ></TD
  67. ></TR
  68. ></TABLE
  69. ><HR
  70. ALIGN="LEFT"
  71. WIDTH="100%"></DIV
  72. ><DIV
  73. CLASS="reference"
  74. ><A
  75. NAME="ref.overload"
  76. ></A
  77. ><DIV
  78. CLASS="TITLEPAGE"
  79. ><H1
  80. CLASS="title"
  81. >LXXVI. Object property and method call overloading</H1
  82. ><DIV
  83. CLASS="PARTINTRO"
  84. ><A
  85. NAME="AEN70322"
  86. ></A
  87. ><DIV
  88. CLASS="section"
  89. ><H1
  90. CLASS="section"
  91. ><A
  92. NAME="overload.intro"
  93. ></A
  94. >Introduction</H1
  95. ><P
  96. >&#13; The purpose of this extension is to allow overloading of object
  97. property access and method calls. Only one function is defined
  98. in this extension, <A
  99. HREF="function.overload.html"
  100. ><B
  101. CLASS="function"
  102. >overload()</B
  103. ></A
  104. > which
  105. takes the name of the class that should have this functionality
  106. enabled. The class named has to define appropriate methods if
  107. it wants to have this functionality: <TT
  108. CLASS="literal"
  109. >__get()</TT
  110. >,
  111. <TT
  112. CLASS="literal"
  113. >__set()</TT
  114. > and <TT
  115. CLASS="literal"
  116. >__call()</TT
  117. >
  118. respectively for getting/setting a property, or calling a method.
  119. This way overloading can be selective. Inside these handler
  120. functions the overloading is disabled so you can access object
  121. properties normally.
  122. </P
  123. ><DIV
  124. CLASS="warning"
  125. ><P
  126. ></P
  127. ><TABLE
  128. CLASS="warning"
  129. BORDER="1"
  130. WIDTH="100%"
  131. ><TR
  132. ><TD
  133. ALIGN="CENTER"
  134. ><B
  135. >Warning</B
  136. ></TD
  137. ></TR
  138. ><TR
  139. ><TD
  140. ALIGN="LEFT"
  141. ><P
  142. >This extension is
  143. <SPAN
  144. CLASS="emphasis"
  145. ><I
  146. CLASS="emphasis"
  147. >EXPERIMENTAL</I
  148. ></SPAN
  149. >. The behaviour of this extension --
  150. including the names of its functions and anything else documented
  151. about this extension -- may change without notice in a future release of PHP.
  152. Use this extension at your own risk.</P
  153. ></TD
  154. ></TR
  155. ></TABLE
  156. ></DIV
  157. ></DIV
  158. ><DIV
  159. CLASS="section"
  160. ><H1
  161. CLASS="section"
  162. ><A
  163. NAME="overload.requirements"
  164. ></A
  165. >Requirements</H1
  166. ><P
  167. >No external libraries are needed to build this extension.</P
  168. ></DIV
  169. ><DIV
  170. CLASS="section"
  171. ><H1
  172. CLASS="section"
  173. ><A
  174. NAME="overload.installation"
  175. ></A
  176. >Installation</H1
  177. ><P
  178. >&#13; In order to use these functions, you must compile
  179. PHP with the <TT
  180. CLASS="option"
  181. >--enable-overload</TT
  182. > option.
  183. Starting with PHP 4.3.0 this extension is enabled by default. You can
  184. disable overload support with
  185. <TT
  186. CLASS="option"
  187. >--disable--overload</TT
  188. >.
  189. </P
  190. ><P
  191. > The windows version of <TT
  192. CLASS="literal"
  193. >PHP</TT
  194. >
  195. has built in support for this extension. You do not need to load any additional
  196. extension in order to use these functions.</P
  197. ><DIV
  198. CLASS="note"
  199. ><BLOCKQUOTE
  200. CLASS="note"
  201. ><P
  202. ><B
  203. >Note: </B
  204. >
  205. Builtin support for overload is available with PHP 4.3.0.
  206. </P
  207. ></BLOCKQUOTE
  208. ></DIV
  209. ></DIV
  210. ><DIV
  211. CLASS="section"
  212. ><H1
  213. CLASS="section"
  214. ><A
  215. NAME="overload.configuration"
  216. ></A
  217. >Runtime Configuration</H1
  218. ><P
  219. >This extension has no configuration directives defined in <TT
  220. CLASS="filename"
  221. >php.ini</TT
  222. >.</P
  223. ></DIV
  224. ><DIV
  225. CLASS="section"
  226. ><H1
  227. CLASS="section"
  228. ><A
  229. NAME="overload.resources"
  230. ></A
  231. >Resource Types</H1
  232. ><P
  233. >This extension has no resource types defined.</P
  234. ></DIV
  235. ><DIV
  236. CLASS="section"
  237. ><H1
  238. CLASS="section"
  239. ><A
  240. NAME="overload.constants"
  241. ></A
  242. >Predefined Constants</H1
  243. ><P
  244. >This extension has no constants defined.</P
  245. ></DIV
  246. ><DIV
  247. CLASS="section"
  248. ><H1
  249. CLASS="section"
  250. ><A
  251. NAME="overload.examples"
  252. ></A
  253. >Examples</H1
  254. ><P
  255. >&#13; Some simple examples on using the <A
  256. HREF="function.overload.html"
  257. ><B
  258. CLASS="function"
  259. >overload()</B
  260. ></A
  261. >
  262. function:
  263. <TABLE
  264. WIDTH="100%"
  265. BORDER="0"
  266. CELLPADDING="0"
  267. CELLSPACING="0"
  268. CLASS="EXAMPLE"
  269. ><TR
  270. ><TD
  271. ><DIV
  272. CLASS="example"
  273. ><A
  274. NAME="AEN70359"
  275. ></A
  276. ><P
  277. ><B
  278. >Example 1. Overloading a PHP class</B
  279. ></P
  280. ><TABLE
  281. BORDER="0"
  282. BGCOLOR="#E0E0E0"
  283. CELLPADDING="5"
  284. ><TR
  285. ><TD
  286. ><PRE
  287. CLASS="php"
  288. >&#60;?php
  289. class OO
  290. {
  291. var $a = 111;
  292. var $elem = array('b' =&#62; 9, 'c' =&#62; 42);
  293. // Callback method for getting a property
  294. function __get($prop_name, &#38;$prop_value)
  295. {
  296. if (isset($this-&#62;elem[$prop_name])) {
  297. $prop_value = $this-&#62;elem[$prop_name];
  298. return true;
  299. } else {
  300. return false;
  301. }
  302. }
  303. // Callback method for setting a property
  304. function __set($prop_name, $prop_value)
  305. {
  306. $this-&#62;elem[$prop_name] = $prop_value;
  307. return true;
  308. }
  309. }
  310. // Here we overload the OO object
  311. overload('OO');
  312. $o = new OO;
  313. print "\$o-&#62;a: $o-&#62;a\n"; // print: $o-&#62;a:
  314. print "\$o-&#62;b: $o-&#62;b\n"; // print: $o-&#62;b: 9
  315. print "\$o-&#62;c: $o-&#62;c\n"; // print: $o-&#62;c: 42
  316. print "\$o-&#62;d: $o-&#62;d\n"; // print: $o-&#62;d:
  317. // add a new item to the $elem array in OO
  318. $o-&#62;x = 56;
  319. // instantiate stdclass (it is built-in in PHP 4)
  320. // $val is not overloaded!
  321. $val = new stdclass;
  322. $val-&#62;prop = 555;
  323. // Set "a" to be an array with the $val object in it
  324. // But __set() will put this in the $elem array
  325. $o-&#62;a = array($val);
  326. var_dump($o-&#62;a[0]-&#62;prop);
  327. ?&#62;</PRE
  328. ></TD
  329. ></TR
  330. ></TABLE
  331. ></DIV
  332. ></TD
  333. ></TR
  334. ></TABLE
  335. >
  336. </P
  337. ><DIV
  338. CLASS="warning"
  339. ><P
  340. ></P
  341. ><TABLE
  342. CLASS="warning"
  343. BORDER="1"
  344. WIDTH="100%"
  345. ><TR
  346. ><TD
  347. ALIGN="CENTER"
  348. ><B
  349. >Warning</B
  350. ></TD
  351. ></TR
  352. ><TR
  353. ><TD
  354. ALIGN="LEFT"
  355. ><P
  356. >&#13; As this is an experimental extension, not all things
  357. work. There is no <TT
  358. CLASS="literal"
  359. >__call()</TT
  360. > support
  361. currently, you can only overload the get and set
  362. operations for properties. You cannot invoke the
  363. original overloading handlers of the class, and
  364. <TT
  365. CLASS="literal"
  366. >__set()</TT
  367. > only works to one level
  368. of property access.
  369. </P
  370. ></TD
  371. ></TR
  372. ></TABLE
  373. ></DIV
  374. ></DIV
  375. ></DIV
  376. ><DIV
  377. CLASS="TOC"
  378. ><DL
  379. ><DT
  380. ><B
  381. >Table of Contents</B
  382. ></DT
  383. ><DT
  384. ><A
  385. HREF="function.overload.html"
  386. >overload</A
  387. >&nbsp;--&nbsp;Enable property and method call overloading for a class</DT
  388. ></DL
  389. ></DIV
  390. ></DIV
  391. ></DIV
  392. ><DIV
  393. CLASS="NAVFOOTER"
  394. ><HR
  395. ALIGN="LEFT"
  396. WIDTH="100%"><TABLE
  397. SUMMARY="Footer navigation table"
  398. WIDTH="100%"
  399. BORDER="0"
  400. CELLPADDING="0"
  401. CELLSPACING="0"
  402. ><TR
  403. ><TD
  404. WIDTH="33%"
  405. ALIGN="left"
  406. VALIGN="top"
  407. ><A
  408. HREF="function.ob-start.html"
  409. ACCESSKEY="P"
  410. >Prev</A
  411. ></TD
  412. ><TD
  413. WIDTH="34%"
  414. ALIGN="center"
  415. VALIGN="top"
  416. ><A
  417. HREF="index.html"
  418. ACCESSKEY="H"
  419. >Home</A
  420. ></TD
  421. ><TD
  422. WIDTH="33%"
  423. ALIGN="right"
  424. VALIGN="top"
  425. ><A
  426. HREF="function.overload.html"
  427. ACCESSKEY="N"
  428. >Next</A
  429. ></TD
  430. ></TR
  431. ><TR
  432. ><TD
  433. WIDTH="33%"
  434. ALIGN="left"
  435. VALIGN="top"
  436. >ob_start</TD
  437. ><TD
  438. WIDTH="34%"
  439. ALIGN="center"
  440. VALIGN="top"
  441. ><A
  442. HREF="funcref.html"
  443. ACCESSKEY="U"
  444. >Up</A
  445. ></TD
  446. ><TD
  447. WIDTH="33%"
  448. ALIGN="right"
  449. VALIGN="top"
  450. >overload</TD
  451. ></TR
  452. ></TABLE
  453. ></DIV
  454. ></BODY
  455. ></HTML
  456. >