1 / 12

PHP extras

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 ” ]; } 

norris
Download Presentation

PHP extras

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP extras Some advance elements not required in the project

  2. Error handling • Sofar: • 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; // routeitfurther}

  4. Classes Visibility: public protected private classFirstClass{public$info = 4; // propertypublic functiondisplayInfo()// method {echo $this->info;    }} usage: $a = newFirstClass();$a->displayInfo();

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

  6. 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

  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; }} Acquire resources and set initial values Release resources and notify other objects if needed 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 “<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();

  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 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();

More Related