Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP functions What are Functions? A function structure: <?php function myname( $1arg, $2arg,….. $narg) { echo "Example function.\n"; return $retval; }

Similar presentations


Presentation on theme: "PHP functions What are Functions? A function structure: <?php function myname( $1arg, $2arg,….. $narg) { echo "Example function.\n"; return $retval; }"— Presentation transcript:

1 PHP functions What are Functions? A function structure: <?php function myname( $1arg, $2arg,….. $narg) { echo "Example function.\n"; return $retval; } ?>

2 Eg For Globals <?php $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; ?>

3 Eg For Statics <?php function Tables() { static $count = 0; static $x=0; $y=2; $count++; $x= $x + $y; echo $x ; If ($count < 10) { Tables(); } } ?>

4 Conditional Functions <? function even() { echo "its an even number "; } function odd() { echo"its an odd number "; } $even=false; If ($even == TRUE) even(); Else odd(); ?>

5 Nested Functions <? function fname() { echo "fname "; function lname() { echo "lname "; } //lname(); not accesible fname(); lname(); ?> Here lname() can only be called after calling fname()

6 Function Arguments Passing By Value Eg: <? function add($num) { $sum=$num[0]+$num[1]; echo "$sum "; } $num = array(0,1); add($num); echo $num[0]; ?>

7 Passing By Reference <?php function name(&$string) { $string.= 'lname.'; } $str = 'fname '; name($str); echo $str; // outputs fname lname ?>

8 Use Of Default Parameters Ex: <? function comname ($lname="Softech") { return "company name is $lname "; } echo comname(); echo comname("Relyon");//prints Relyon ?>

9 Continued <? function comname ($fname, $lname="Softech") { return "company name is $fname $lname"; } echo comname("Relyon");//prints Relyon Softech ?>

10 Variable Length Argument List and Variable Functions <? function add($a,$b) { $numargs=func_num_args(); if($numargs>=2) print("the second arg is ".func_get_arg(1)." \n"); print("the no of arguments passed to this function are $numargs"); echo" "; $arg_list=func_get_args();

11 Continued for($i=0;$i<$numargs;$i++) { echo "Argument $i is: ". $arg_list[$i]. " \n"; } $c=$a+$b; return $c; } $d='add'; $e=$d(2,10); echo " sum=$e "; ?>

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->a;//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. <? Class name { public $a=1; public function display() { echo $this->a; } $b = new name; $b->display(); ?>

14 Extends and Constructors <?php class parentclass { // member declaration public $name = 'Relyon'; // method declaration public function display() { echo $this->name; }

15 Continued class childclass extends parentclass { function display() { echo "Extending company\n"; parent::display(); } $extended = new childclass(); $extended->display(); ?>

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: <?class fname { final public function name() { echo relyon ;} class lname extends fname { public function name() { echo softech ;} } ?>

17 Visibility Private Protected public

18 Ex For Visibility <? class firstone { public $public = "public"; protected $protected = "protected"; private $private = "private"; private function privateone() { echo "hi "; } protected function protectedone() { echo"protected "; } $ob = new firstone; $ob->public;

19 continued //$ob->protected; //$ob->private; //$ob->privateone(); class secondone extends firstone { public function hello() { $this->protected; $this->protectedone(); } $c=new secondone; $c->hello(); ?>

20 Scope Resolution Operator(::) <? class name { const name1 = 'relyon '; } echo name::name1; class sname extends name { public static $sname = "ltd"; public static function display() { echo parent::name1; echo self::$sname; } sname::display(); ?>

21 Abstract Classes <?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->getname(). "\n"; }

22 Continued class fname1 extends name { public function getname() { return "Relyon"; } public function prefixname($prefix) { return "$prefix"; } $class1 = new fname1; $class1->display(); echo $class1->prefixname('softech')."\n"; ?>


Download ppt "PHP functions What are Functions? A function structure: <?php function myname( $1arg, $2arg,….. $narg) { echo "Example function.\n"; return $retval; }"

Similar presentations


Ads by Google