• PUDN用户
  • PHP
  • 181KB
  • zip
  • 0
  • 1 积分
  • 4
  • 2009-11-23 16:50
PHP 电子书 让读者 学习object oriented programming
  • PHP_Objects.pdf
<html xmlns=""> <head> <meta charset="utf-8"> <meta name="generator" content="pdf2htmlEX"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <link rel="stylesheet" href=""> <link rel="stylesheet" href=""> <link rel="stylesheet" href=""> <script src=""></script> <script src=""></script> <script> try{ pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({}); }catch(e){} </script> <title></title> </head> <body> <div id="sidebar" style="display: none"> <div id="outline"> </div> </div> <div id="pf1" class="pf w0 h0" data-page-no="1"><div class="pc pc1 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src=""><div class="t m0 x1 h2 y1 ff1 fs0 fc0 sc0 ls0 ws0">PHP Back to Basics &#8211; Working with<span class="_ _0"></span> Objects </div><div class="t m0 x2 h3 y2 ff1 fs1 fc1 sc0 ls1 ws1">Gareth Downes-Powell </div><div class="t m0 x3 h2 y3 ff1 fs0 fc0 sc0 ls1 ws2"> </div><div class="t m0 x4 h4 y4 ff1 fs2 fc1 sc0 ls2 ws3">PHP Back to Basics &#8211; Part 9 &#8211; Working with Objects </div><div class="t m0 x4 h5 y5 ff2 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x4 h5 y6 ff2 fs0 fc1 sc0 ls3 ws4">In this tutorial we're going to look at working with o<span class="ls4 ws5">bjects in PHP.<span class="_ _0"></span> Objects are invaluable when i<span class="_ _0"></span>t comes to </span></div><div class="t m0 x4 h5 y7 ff2 fs0 fc1 sc0 ls5 ws6">organizing and sharing your code, and allow you to easily cre<span class="_ _1"></span>ate libraries of code that can be used in many </div><div class="t m0 x4 h5 y8 ff2 fs0 fc1 sc0 ls5 ws6">different projects without changes needing to be made. To<span class="_ _1"></span><span class="ls6 ws7"> start the tutorial, we're going to l<span class="_ _1"></span>ook at what an </span></div><div class="t m0 x4 h5 y9 ff2 fs0 fc1 sc0 ls7 ws8">object actually is. </div><div class="t m0 x4 h5 ya ff2 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x4 h6 yb ff3 fs3 fc1 sc0 ls8 ws9">What is an Object? </div><div class="t m0 x4 h5 yc ff2 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x4 h5 yd ff2 fs0 fc1 sc0 ls9 wsa">An object is a set of variables and functions <span class="lsa wsb">con<span class="_ _0"></span>tained in a special co<span class="_ _0"></span>de template c<span class="_ _0"></span>alled a <span class="ff4 lsb ws2">class</span><span class="lsc wsc">. One of the </span></span></div><div class="t m0 x4 h5 ye ff2 fs0 fc1 sc0 lsc wsc">best things about objec<span class="_ _0"></span>ts is the fact tha<span class="_ _0"></span>t they hide the workin<span class="_ _0"></span>gs of the code away from the <span class="_ _0"></span>end user, so you </div><div class="t m0 x4 h5 yf ff2 fs0 fc1 sc0 lsd wsd">can use a class without ac<span class="_ _0"></span>tually knowin<span class="lse wse">g how the code insid<span class="_ _0"></span>e the class works.<span class="lsf wsf"> This is a similar principle to a </span></span></div><div class="t m0 x4 h5 y10 ff2 fs0 fc1 sc0 ls10 ws10">car, for example;<span class="_ _0"></span> you can drive a ca<span class="_ _0"></span>r and use it, withou<span class="ls7 ws8">t ac<span class="_ _0"></span>tually having to know how the engine works. </span></div><div class="t m0 x4 h5 y11 ff2 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x4 h5 y12 ff2 fs0 fc1 sc0 ls0 ws11">Because the code is self contained, objects are reus<span class="ls11 ws12">able a<span class="_ _0"></span>nd can also be used easily with a range of </span></div><div class="t m0 x4 h5 y13 ff2 fs0 fc1 sc0 ls12 ws13">different projects, saving you hav<span class="_ _1"></span>ing to keep recoding the same<span class="_ _1"></span> things over and over again. </div><div class="t m0 x4 h5 y14 ff2 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x4 h5 y15 ff2 fs0 fc1 sc0 ls13 ws14">Before we cont<span class="_ _1"></span>inue, we need to understand the diffe<span class="ls5 ws6">rence betwee<span class="_ _1"></span>n a class and an object. A class is the </span></div><div class="t m0 x4 h5 y16 ff2 fs0 fc1 sc0 ls14 ws15">code itself,<span class="_ _0"></span> in the special cla<span class="_ _0"></span>ss format. To use <span class="_ _0"></span>the class, <span class="ls1 ws16">we crea<span class="_ _0"></span>te an instance of th<span class="ls9 wsa">e class, and this is called </span></span></div><div class="t m0 x4 h5 y17 ff2 fs0 fc1 sc0 ls7 ws8">an object. You'll see how <span class="_ _0"></span>to create a class, and th<span class="ls15 ws17">en crea<span class="_ _0"></span>te an object from the c<span class="_ _0"></span>lass as you progress </span></div><div class="t m0 x4 h5 y18 ff2 fs0 fc1 sc0 lsc ws18">through this tutorial. </div><div class="t m0 x4 h5 y19 ff2 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x4 h6 y1a ff3 fs3 fc1 sc0 ls16 ws19">Creating a Class </div><div class="t m0 x4 h5 y1b ff2 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x4 h5 y1c ff2 fs0 fc1 sc0 ls7 ws8">To create a class, we must create <span class="_ _0"></span>the code in a <span class="ls17 ws1a">special format which is shown bel<span class="_ _1"></span>ow at its simplest<span class="_ _1"></span>. </span></div><div class="t m0 x4 h5 y1d ff2 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x4 h7 y1e ff5 fs0 fc1 sc0 ls18 ws2">class className { </div><div class="t m0 x4 h7 y1f ff5 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x4 h7 y20 ff5 fs0 fc1 sc0 ls18 ws2">} </div><div class="t m0 x4 h5 y21 ff2 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x4 h5 y22 ff2 fs0 fc1 sc0 ls19 ws1b">Here, we use <span class="_ _0"></span>the <span class="ff5 ls18 ws2">class</span><span class="ls15 ws1c"> keyword so that PHP knows that we're cr<span class="ls1a ws1d">eating a class,<span class="_ _0"></span> and we give it a name </span></span></div><div class="t m0 x4 h5 y23 ff4 fs0 fc1 sc0 lsd ws2">className<span class="ff2 ls1b ws1e"> which w<span class="_ _0"></span>e will use <span class="_ _0"></span>to refer<span class="_ _0"></span> to that class<span class="_ _0"></span>. All th<span class="_ _0"></span><span class="ls12 ws13">e code for the class is cont<span class="_ _1"></span>ained inside a set of </span></span></div><div class="t m0 x4 h5 y24 ff2 fs0 fc1 sc0 ls1c ws2">braces <span class="ff6 ls18">{ }<span class="ff2">. This defines a very basic class, which is <span class="ls1 ws16">actually useless, as it doesn't actually </span><span class="ff4 ls17">do</span><span class="ls6 ws7"> any thing! A </span></span></span></div><div class="t m0 x4 h5 y25 ff2 fs0 fc1 sc0 ls7 ws8">class only becomes useful once w<span class="_ _0"></span>e have given it some<span class="ls1d ws1f"> properties and functions, which we'll look at once </span></div><div class="t m0 x4 h5 y26 ff2 fs0 fc1 sc0 ls4 ws20">we've looked at how to create<span class="ls1e ws21"> a new object from a class. </span></div><div class="t m0 x4 h5 y27 ff2 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x4 h8 y28 ff1 fs4 fc1 sc0 ls1f ws22">Initiating a Class &#8211; Creating an Object </div><div class="t m0 x4 h5 y29 ff2 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x4 h5 y2a ff2 fs0 fc1 sc0 ls3 ws4">To create a new object, we need to initiate a new instance of the class as shown below. </div><div class="t m0 x4 h5 y2b ff2 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x4 h7 y2c ff5 fs0 fc1 sc0 ls18 ws2">$object = new className(); </div><div class="t m0 x4 h5 y2d ff2 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x4 h5 y2e ff5 fs0 fc1 sc0 ls18 ws2">$object<span class="ff2 lsc wsc"> can now be used to ref<span class="_ _0"></span>er to the class, and can be us<span class="_ _0"></span>ed to call any of the c<span class="_ _0"></span>lass's properties or </span></div><div class="t m0 x4 h5 y2f ff2 fs0 fc1 sc0 lsc ws2">methods. </div><div class="t m0 x4 h5 y30 ff2 fs0 fc1 sc0 ls1 ws2"> </div><div class="t m0 x5 h9 y31 ff2 fs5 fc1 sc0 ls20 ws23">Copyright &#169; 2004<span class="ls21 ws24">m All Rights Reserved </span></div><div class="t m0 x6 h9 y32 ff2 fs5 fc1 sc0 ls22 ws25">to get mo<span class="_ _1"></span>re go to<span class="_ _1"></span> DMXzo<span class="_ _1"></span>ne.c<span class="_ _1"></span>om<span class="_ _1"></span> </div><div class="t m0 x7 h5 y33 ff2 fs0 fc1 sc0 lsc wsc">Page 1 of 9 </div></div><div class="pi" data-data='{"ctm":[1.568627,0.000000,0.000000,1.568627,0.000000,0.000000]}'></div></div> </body> </html>