Page 243 - Beginning PHP 5.3
P. 243

Chapter 8: Objects
                           You can then make a class implement an interface using the  implements  keyword:

                             class MyClass implements MyInterface {
                               public function myMethod1( $param1, $param2 ) {
                                 // (implement the method here)
                               }

                               public function myMethod2( $param1, $param2 ) {
                                 // (implement the method here)
                               }

                             }
                           To implement more than one interface at once, separate the interface names with commas:
                             class MyClass implements MyInterface1, MyInterface2 {





                                     Try It Out   Create and Use an Interface
                           The following example shows how to create and use a  Sellable  interface to turn two quite unrelated

                         classes  —   Television  and  TennisBall  —  into sellable items in an online store. Save the script as

                           interfaces.php  in your document root folder and open it in your browser; you should see the result
                         shown in Figure  8 - 7 .

                               < !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
                               “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd” >
                               < html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en” >
                                < head >
                                  < title > Creating and Using an Interface < /title >
                                  < link rel=”stylesheet” type=”text/css” href=”common.css” / >
                                < /head >
                                < body >
                                  < h1 > Creating and Using an Interface < /h1 >

                               < ?php

                             interface Sellable {
                               public function addStock( $numItems );
                               public function sellItem();
                               public function getStockLevel();
                             }

                             class Television implements Sellable {
                               private $_screenSize;
                               private $_stockLevel;

                               public function getScreenSize() {
                                 return $this- > _screenSize;
                               }




                                                                                                         205





                                                                                                      9/21/09   9:03:44 AM
          c08.indd   205
          c08.indd   205                                                                              9/21/09   9:03:44 AM
   238   239   240   241   242   243   244   245   246   247   248