Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP extras Some advance elements not required in the project.

Similar presentations


Presentation on theme: "PHP extras Some advance elements not required in the project."— Presentation transcript:

1 PHP extras Some advance elements not required in the project

2 Error handling So far: – die ($status) – exit ($status) - Terminates execution of the script – @

3 Error handling Try, catch, throw try { $a=$_POST[“val1”]/$_POST[“val2”]; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(); throw $e; // route it further }

4 Classes class FirstClass { public $info = 4; // property public function displayInfo() // method { echo $this->info; } } usage: $a = new FirstClass(); $a->displayInfo(); Visibility: public protected private

5 Inheritance class SecondClass extends FirstClass { public function displayHalfInfo() // method { echo ($this->info/2); } }

6 Inheritance class GenClass { public $salary= 40000; public function displayPaidSalary() { echo $this->salary; } } class EstClass extends GenClass { public function displayPaidSalary() { echo $this->salary*0.78; } } redefines the function $a=new GenClass(); $b=new EstClass(); // shows 10000 $a->displaySalary(); // shows 7800 $b->displaySalary();

7 Constructor class FClass { function __construct() { echo “first class constructor"; } function __destruct() { print "Destroying first class"; } } class SClass extends FClass { protected $val=0; function __construct() { parent::__construct(); print “second class constructor\n"; } function __construct($initVal){ $this->$val=$initval; } } Release resources and notify other objects if needed Acquire resources and set initial values Parent construtor is called directly

8 Static (shared) class General { public static $my_shared = 55; public static function StaticMethod() { //... } } echo General::my_shared; echo General::StaticMethod;

9 Interface interface iOutput { public function getHtml(); } class Student implement iOutput { private $mName=“Name1”; private $mCode=“098132”; public function getHtml() { echo “ $mCode /”; echo “ $mName ”; } class Teacher implement iOutput { private $mName=“Name1”; private $mPosition=“Prof”; public function getHtml() { echo “$nPosition $mName ”; } $arr[0]=new Student(); $arr[1]=new Teacher(); foreach($arr as $value) $value->getHtml();

10 Abstraction abstract class AbstractClass { abstract protected function getValue(); public function printOut() { print $this->getValue(). "\n"; } } class ConcreteClass1 extends AbstractClass { protected function getValue() { return "ConcreteClass1"; } } class ConcreteClass2 extends AbstractClass { public function getValue() { return "ConcreteClass2"; } }

11 Namespaces A possibility to encapsulate and divide similar concepts by the area of usage. For example there could be an “operation” concept, which means one in math and another in medicine namespace CompanyName\Finance; class Account { static function getHtml() {} } namespace CompanyName\Security; class Account { static function getHtml() {} }

12 Namespaces: usage calling echo CompanyName\Finance\Account::getHtml(); echo CompanyName\Security\Account::getHtml(); calling use CompanyName\Finance; echo Account::getHtml(); namespace CompanyName\Finance; class Account { static function getHtml() {} } namespace CompanyName\Security; class Account { static function getHtml() {} }


Download ppt "PHP extras Some advance elements not required in the project."

Similar presentations


Ads by Google