Page 126 - Open Soource Technologies 304.indd
P. 126

Unit 7: Functions



                             {                                                                    Notes

                               public function printItem($string)
                                 {

                                      echo ‘Foo: ‘ . $string . PHP_EOL;
                                  }

                                      public function printPHP()
                                 {

                                      echo ‘PHP is great.’ . PHP_EOL;
                                  }

                             }


                          class bar extends foo

                          {
                               public function printItem($string)
                               {

                                   echo ‘Bar: ‘ . $string . PHP_EOL;
                                 }

                          }


                          $foo = new foo();

                          $bar = new bar();
                          $foo->printItem(‘baz’);  // Output: ‘Foo: baz’
                          $foo->printPHP(); // Output: ‘PHP is great’

                          $bar->printItem(‘baz’);  // Output: ‘Bar: baz’
                          $bar->printPHP(); // Output: ‘PHP is great’

                          ?>
            7.10 Summary

               •  The general way of defining a function is

                 Function function_name (arg1, arg2, arg3,………).
               •  Every  function  has  its  own  set  of  variables.  Any  variable  used  outside  the  function’s
                 definition are not accessible from within the function by default.
               •  The return statement returns value by value, which means that a copy of the value is
                 created and is returned to the caller of the function.
               •  Default parameters enable you to specify a default value for function parameters that are
                 not passed to the function during the function call.


                                             LOVELY PROFESSIONAL UNIVERSITY                                   121
   121   122   123   124   125   126   127   128   129   130   131