PHP functions What are Functions? A function structure: <?php function myname( $1arg, $2arg,….. $narg) { echo "Example function.\n"; return $retval; } ?>
Eg For Globals <?php $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; ?>
Eg For Statics <?php function Tables() { static $count = 0; static $x=0; $y=2; $count++; $x= $x + $y; echo $x ; If ($count < 10) { Tables(); } } ?>
Conditional Functions <? function even() { echo "its an even number "; } function odd() { echo"its an odd number "; } $even=false; If ($even == TRUE) even(); Else odd(); ?>
Nested Functions <? function fname() { echo "fname "; function lname() { echo "lname "; } //lname(); not accesible fname(); lname(); ?> Here lname() can only be called after calling fname()
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]; ?>
Passing By Reference <?php function name(&$string) { $string.= 'lname.'; } $str = 'fname '; name($str); echo $str; // outputs fname lname ?>
Use Of Default Parameters Ex: <? function comname ($lname="Softech") { return "company name is $lname "; } echo comname(); echo comname("Relyon");//prints Relyon ?>
Continued <? function comname ($fname, $lname="Softech") { return "company name is $fname $lname"; } echo comname("Relyon");//prints Relyon Softech ?>
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();
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 "; ?>
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; }
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(); ?>
Extends and Constructors <?php class parentclass { // member declaration public $name = 'Relyon'; // method declaration public function display() { echo $this->name; }
Continued class childclass extends parentclass { function display() { echo "Extending company\n"; parent::display(); } $extended = new childclass(); $extended->display(); ?>
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 ;} } ?>
Visibility Private Protected public
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;
continued //$ob->protected; //$ob->private; //$ob->privateone(); class secondone extends firstone { public function hello() { $this->protected; $this->protectedone(); } $c=new secondone; $c->hello(); ?>
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(); ?>
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"; }
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"; ?>