120 likes | 250 Views
PHP extras. Some advance elements not required in the project. Error handling. So far : die ( $status ) exit ( $status ) - Terminates execution of the script @. Error handling. Try , catch , throw try { $a=$_POST[“val1 ” ]/$_POST[“ val2 ” ]; }
E N D
PHP extras Some advance elements not required in the project
Error handling • Sofar: • die ($status) • exit ($status) - Terminates execution of the script • @
Error handling • Try, catch , throw try {$a=$_POST[“val1”]/$_POST[“val2”];} catch (Exception $e) {echo 'Caught exception: ', $e->getMessage(); throw$e; // routeitfurther}
Classes Visibility: public protected private classFirstClass{public$info = 4; // propertypublic functiondisplayInfo()// method {echo $this->info; }} usage: $a = newFirstClass();$a->displayInfo();
Inheritance class SecondClass extends FirstClass{ public function displayHalfInfo() // method { echo ($this->info/2); }}
Inheritance $a=new GenClass(); $b=new EstClass(); // shows 10000 $a->displaySalary(); // shows 7800 $b->displaySalary(); 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
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; }} Acquire resources and set initial values Release resources and notify other objects if needed Parent construtor is called directly
Static (shared) class General { public static $my_shared = 55; public static function StaticMethod() { // ... }} echo General::my_shared; echo General::StaticMethod;
Interface interface iOutput { public function getHtml(); } class Student implement iOutput { private $mName=“Name1”; private $mCode=“098132”; public function getHtml() { echo “<i> $mCode</i> /”; echo “<b> $mName</b>”; } } class Teacher implement iOutput { private $mName=“Name1”; private $mPosition=“Prof”; public function getHtml() { echo “$nPosition<b> $mName</b>”; } } $arr[0]=new Student(); $arr[1]=new Teacher(); foreach($arr as $value) $value->getHtml();
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"; }}
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() {}}
Namespaces: usage namespace CompanyName\Finance;class Account{static function getHtml() {}} namespace CompanyName\Security;class Account{static function getHtml() {}} • calling echo CompanyName\Finance\Account::getHtml(); echo CompanyName\Security\Account::getHtml(); • calling use CompanyName\Finance; echo Account::getHtml();