Advance Techniques In Php

2,065 views
1,917 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,065
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Advance Techniques In Php

  1. 1. Advance Techniques in PHP <ul><li>Backtracing </li></ul><ul><li>Method Chaining </li></ul>Kumar S
  2. 2. Backtracing: debug_backtrace() <ul><li>function bar() { print_r(debug_backtrace()); } bar('apple', 'banana', 'pear'); </li></ul><ul><ul><li>[0] => Array ( [file] => /example.php [line] => 12 [function] => foo [args] => Array ( [0] => apple [1] => banana [2] => pear ) ) </li></ul></ul>Kumar S
  3. 3. Backtracing: debug_backtrace() <ul><li>$backtrace = print_r(debug_backtrace(), true); This will email or log the backtrace </li></ul>Kumar S
  4. 4. Backtracing: debug_print_backtrace() <ul><li>If debug_print_backtrace() was called in the same place in the example script above in place of debug_backtrace() it would output the following: </li></ul><ul><li>#0 bar() called at [-example.php:4] #1 foo(apple, banana, pear) called at [-example.php:12] </li></ul>Kumar S
  5. 5. Capture the info from debug_print_backtrace <ul><li>Using debug_print_backtrace with output buffering ob_start(); debug_print_backtrace(); $backtrace = ob_get_clean(); </li></ul><ul><li>Using the Exception object $e = new Exception(); $backtrace = $e->getTraceAsString(); </li></ul>Kumar S
  6. 6. Method Chaining <ul><li>A Typical Class class Person {     private $m_szName;     private $m_iAge;          public function setName($szName)     {         $this->m_szName = $szName;     }          public function setAge($iAge)     {         $this->m_iAge = $iAge;     }          public function  getDetails ()     {         printf(             'Hello my name is %s and I am %d years old.',             $this->m_szName,             $this->m_iAge);     } } </li></ul><ul><li>$p1 = new Person(); $p1->setName(‘Kumar'); $p1->setAge(30); $p1->getDetails(); </li></ul>Kumar S
  7. 7. Method Chaining <ul><li>class Person {     private $m_szName;     private $m_iAge;          public function setName($szName)     {         $this->m_szName = $szName;         return $this; // We now return $this (the Person)     }          public function setAge($iAge)     {         $this->m_iAge = $iAge;         return $this; // Again, return our Person     }          public function getDetails()     {         printf(             'Hello my name is %s and I am %d years old.',             $this->m_szName,             $this->m_iAge);     } }  </li></ul>Kumar S
  8. 8. Using Method Chaining <ul><li>$p1 = new Person(); $p1->setName(‘Kumar')->setAge(30)->getDetails();  </li></ul>Kumar S
  9. 9. How it works <ul><li>First up is $p1->setName(‘Kumar'). This assigns the person's name to be Kumar and returns $this -- that is, the $p1 object. So at the moment Kumar has his name, but no age! Next up comes ->setAge(30). Because we're chained with the previous method, PHP interprets the code and says &quot;execute the setAge method belonging to whatever was returned from the previous method.&quot; In this case, PHP executes the setAge method belonging to our Person object, $Kumar. </li></ul><ul><li>Recall how we do use trim functions $x = ltrim(rtrim(“ Some String “)); </li></ul>Kumar S

×