PHP functions - PowerPoint PPT Presentation

About This Presentation
Title:

PHP functions

Description:

A class is a collection of variables and functions which work on this variables. ... ob- protected; //$ob- private; //$ob- privateone(); class secondone extends ... – PowerPoint PPT presentation

Number of Views:105
Avg rating:3.0/5.0
Slides: 23
Provided by: Avi86
Category:

less

Transcript and Presenter's Notes

Title: PHP functions


1
PHP functions
  • What are Functions?
  • A function structure
  • lt?php
  • function myname( 1arg, 2arg,.. narg)
  •    echo "Example function.\n"   return
    retval
  • ?gt

2
Eg For Globals
  • lt?php
  • a 1b 2
  • function Sum()
  •    global a, b    b a b
    Sum()echo b
  • ?gt

3
Eg For Statics
  • lt?php
  • function Tables()
  •    static count 0 static x0
  • y2
  • count
  •  x x y
  • echo x ltbrgt
  •   If (count lt 10)
  •         Tables()   
  • ?gt

4
Conditional Functions
  • lt?
  • function even()
  • echo "its an even numberltbrgt"
  • function odd()
  • echo"its an odd numberltbrgt"
  • evenfalse
  • If (even TRUE)
  • even()
  • Else
  • odd()
  • ?gt

5
Nested Functions
  • lt?
  • function fname()
  • echo "fnameltbrgt"
  • function lname()
  • echo "lnameltbrgt"
  • //lname() not accesible
  • fname()
  • lname()
  • ?gt
  • Here lname() can only be called after calling
    fname()

6
Function Arguments
  • Passing By Value
  • Eg
  • lt?
  • function add(num)
  • sumnum0num1
  • echo "sumltbrgt"
  • num array(0,1)
  • add(num)
  • echo num0
  • ?gt

7
Passing By Reference
  • lt?php
  • function name(string)
  • string . 'lname.'
  • str 'fname '
  • name(str)
  • echo str // outputs fname lname
  • ?gt

8
Use Of Default Parameters
  • Ex
  • lt?
  • function comname (lname"Softech")
  • return "company name is lnameltbrgt"
  • echo comname()
  • echo comname("Relyon")//prints Relyon
  • ?gt

9
Continued
  • lt?
  • function comname (fname,
    lname"Softech")
  • return "company name is fname lname"
  • echo comname("Relyon")//prints Relyon
    Softech
  • ?gt

10
Variable Length Argument List and Variable
Functions
  • lt?
  • function add(a,b)
  • numargsfunc_num_args()
  • if(numargsgt2)
  • print("the second arg is ".func_get_arg(1)."ltbr/
    gt\n")
  • print("the no of arguments passed to this
    function are numargs")
  • echo"ltbrgt"
  • arg_listfunc_get_args()

11
Continued
  • for(i0iltnumargsi)
  • echo "Argument i is " . arg_listi . "ltbr
    /gt\n"
  • cab
  • return c
  • d'add'
  • ed(2,10)
  • echo "ltbrgtsumeltbrgt"
  • ?gt

12
CLASSES
  • A class is a collection of variables and
    functions which work on this variables.
  • A simple class declaration
  • Class first
  • //member variables
  • public a 1
  • //method declaration
  • public function display()
  • echo this-gta//member methods

13
New
  • New is used to create an instance of a class,
    a new object must be created and assigned to a
    variable.
  • lt?
  • Class name
  • public a1
  • public function display()
  • echo this-gta
  • b new name
  • b-gtdisplay()
  • ?gt

14
Extends and Constructors
  • lt?php
  • class parentclass
  • // member declaration
  • public name 'Relyon'
  • // method declaration
  • public function display()
  • echo this-gtname

15
Continued
  • class childclass extends parentclass
  • function display()
  • echo "Extending company\n"
  • parentdisplay()
  • extended new childclass()
  • extended-gtdisplay()
  • ?gt

16
Final
  • It prevents child classes from overriding a
    method by prefixing the definition with final. If
    the class itself is being defined final then it
    cannot be extended.
  • Ex
  • lt? class fname
  • final public function name()
  • echo relyonltbrgt
  • class lname extends fname
  • public function name()
  • echo softechltbrgt
  • ?gt

17
Visibility
  • Private
  • Protected
  • public

18
Ex For Visibility
  • lt?
  • class firstone
  • public public "public"
  • protected protected "protected"
  • private private "private"
  • private function privateone()
  • echo "hiltbrgt"
  • protected function protectedone()
  • echo"protectedltbrgt"
  • ob new firstone
  • ob-gtpublic

19
continued
  • //ob-gtprotected
  • //ob-gtprivate
  • //ob-gtprivateone()
  • class secondone extends firstone
  • public function hello()
  • this-gtprotected
  • this-gtprotectedone()
  • cnew secondone
  • c-gthello()
  • ?gt

20
Scope Resolution Operator()
  • lt?
  • class name
  • const name1 'relyonltbrgt'
  • echo namename1
  • class sname extends name
  • public static sname "ltd"
  • public static function display()
  • echo parentname1
  • echo selfsname
  • snamedisplay()
  • ?gt

21
Abstract Classes
  • lt?php
  • abstract class name
  • // Force Extending class to define this
    method
  • abstract protected function getname()
  • abstract protected function
    prefixname(prefix)
  • // Common method
  • public function display()
  • print this-gtgetname() . "\n"

22
Continued
  • class fname1 extends name
  • public function getname()
  • return "Relyon"
  • public function prefixname(prefix)
  • return "prefix"
  • class1 new fname1
  • class1-gtdisplay()
  • echo class1-gtprefixname('softech') ."\n"
  • ?gt
Write a Comment
User Comments (0)
About PowerShow.com