O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Starting Out With PHP

Want to learn PHP? This beginner level session will introduce you to PHP and its syntax. Learn how to make a 'Hello World' PHP page, create a class, build a method, declare and use variables, and write control structures (if, else, foreach, switch, etc.). Get excited to start developing in PHP and leave with a foundation to go and build amazing PHP applications on.

  • Entre para ver os comentários

  • Seja a primeira pessoa a gostar disto

Starting Out With PHP

  1. 1. Starting Out With PHP Mark Niebergall
 https://joind.in/talk/fa612
  2. 2. Starting Out With PHP • PHP is a useless language to learn • PHP is hated by all developers • PHP is insecure, slow, and not modern • PHP is bad
  3. 3. Starting Out With PHP • Lets developers make mistakes - $password = md5(‘password123’); - eval($userInput);
  4. 4. Starting Out With PHP • Lets developers make mistakes - echo $$variable . $$$variable; - if (‘true’ == false) {…}
  5. 5. Starting Out With PHP • Lets developers make mistakes - $sql = “DELETE FROM users WHERE id = {$_GET[‘id’]}”; - “INSERT INTO some_data VALUES (NULL, ‘{$_POST[‘name’]}’);
  6. 6. Starting Out With PHP • Lets developers make mistakes - file_get_contents($randomUrl); - $GLOBALS[‘everything’] = $everything;
  7. 7. Starting Out With PHP • Lets developers make mistakes - $oneCase + $two_case + $ThreeCase_More - if (empty($allTheThings)) {…}
  8. 8. Starting Out With PHP • Lets developers make mistakes - if ($a < $b) {
 if ($test == true) {
 if ($number < 100) {
 if (strpos($string, ‘abc’) != 0) {
 if ($nestMore == ‘Yes’) {
 echo ‘Nesting if statements is great!’;
 }
 }
 }
 }
 }
  9. 9. Starting Out With PHP • Lets developers make mistakes - Works on my box!
  10. 10. Starting Out With PHP • PHP runs over 83% of the web • PHP has an active community • PHP is secure, fast, and constantly being updated • PHP is good
  11. 11. About Mark Niebergall • PHP since 2005 • Masters degree in MIS • Senior Software Engineer • Drug screening project • UPHPU President • CSSLP, SSCP Certified and SME • Drones, fishing, skiing, father of 5 boys
  12. 12. Starting Out With PHP • What is PHP • Basic Syntax • Language Features • PHP Ecosystem
  13. 13. What is PHP
  14. 14. What is PHP <?php
  15. 15. What is PHP <?php
 
 echo ‘Hello world!’;
 echo “Hello $name”;
  16. 16. What is PHP <?php
 
 class HelloWorld
 {
 public function sayHello($name)
 {
 echo ‘Hello ‘ . $name;
 }
 }
 
 $helloWorld = new HelloWorld;
 $helloWorld->sayHello(‘OpenWest Attendees’);

  17. 17. What is PHP • Personal Home Page • Rasmus Lerdorf in 1994 • PHP: Hypertext Preprocessor
  18. 18. What is PHP • Programming language designed for the Internet • Server-side scripting language • General-purpose programming language
  19. 19. What is PHP • Facebook • Slack • Tumblr • Wikipedia • News sites • Blogs
  20. 20. What is PHP • Open source - Free to use - Anyone can contribute - Source is public
  21. 21. What is PHP • Written in C • Compiled at run-time
  22. 22. What is PHP
  23. 23. What is PHP
  24. 24. What is PHP • Runs with web servers - Apache - Nginx
  25. 25. What is PHP • Interacts with databases - PostgreSQL - MySQL - MSSQL - Mongo - Many more
  26. 26. What is PHP • APIs • Server • Mail • Files
  27. 27. Starting Out With PHP • What is PHP • Basic Syntax • Language Features • PHP Ecosystem
  28. 28. Basic Syntax <?php
 
 $variable = ‘something’;
 $isSomething = true;
 $someInput = $_GET[‘get_key’];
  29. 29. Basic Syntax <?php
 
 $variable = ‘something’;
 $isSomething = true;
 $someInput = $_GET[‘get_key’];
 
 if ($isSomething) {
 echo $variable;
 }
  30. 30. Basic Syntax <?php
 
 $variable = ‘something’;
 $isSomething = true;
 $someInput = $_GET[‘get_key’];
 
 if ($isSomething === true || $someInput == ‘Yes’) {
 echo $variable;
 }
  31. 31. Basic Syntax <?php
 
 $string = ‘Abc 123’;
 $integer = 123;
 $float = 123.456;
 $bool = true;
 $null = null;
 $array = [123, 456];
 $resource = fopen(‘SomeFile.txt’, ‘r’);
 $object = new SomeClass;
  32. 32. Basic Syntax <?php
 
 $oldArray = array(‘alligator’, ‘bear’, ‘camel’, ‘deer’);
 
 $data = [‘apple’, ‘banana’, ‘carrot’];
 
 $withKeys = [‘test’ => 123, ‘other’ => 456];
 
 foreach ($data as $food) {
 echo ‘Eat a ‘ . $food . PHP_EOL;
 }
  33. 33. Basic Syntax <?php
 
 $whatAmI = ‘Drone’;
 
 switch ($whatAmI) {
 case ‘Airplane’:
 echo ‘Fly far away’;
 break;
 
 case ‘Drone’:
 case ‘Helicopter’:
 echo ‘Fly close by’;
 break;
 }
  34. 34. Basic Syntax <?php
 
 $number = 0;
 
 while ($number < 7) {
 $number += 1;
 }
 
 return $number;
  35. 35. Basic Syntax <?php
 
 for ($i = 0; $i < 100; $i++) {
 …
 }
  36. 36. Basic Syntax <?php
 
 $variable = ‘something’;
 $isSomething = true;
 $someInput = $_GET[‘get_key’];
 
 function doSomething($variable, $isSomething)
 {
 if ($isSomething) {
 return $variable;
 }
 return ‘Not something: ‘ . $something;
 }
  37. 37. Basic Syntax <?php
 
 class Cat{}
  38. 38. Basic Syntax <?php
 
 class Cat {
 protected $name;
 
 public function __construct($name) {
 $this->name = $name;
 }
 
 public function pet() {
 return ‘purr’;
 }
 }
  39. 39. Basic Syntax <?php
 
 class Cat
 {
 // Anything can use public
 public function pet() {…}
 
 // Cat class and anything inheriting can use protected
 protected function speak() {…}
 
 /* only Cat class can use private */
 private function _ignoreHuman() {…}
 }
  40. 40. Basic Syntax <?php
 abstract class Pet {
 protected $name;
 abstract public function feed(Food $food);
 }
 
 class Cat extend Pet {
 public function feed(Food $food) {
 return $this->stare($food);
 }
 protected function stare($thing) {}
 }
  41. 41. Basic Syntax <?php
 
 namespace AnimalPet;
 
 use AnimalHuman;
 
 class Cat
 {
 public function pet() {…}
 protected function speak() {…}
 private function _ignoreHuman(Human $human) {…}
 }
  42. 42. Basic Syntax <?php
 
 include(‘Cat.php’);
 include_once(‘Dog.php’);
 
 require(‘/../Pet.php’);
 require_once(‘/../../Animal.php’);
  43. 43. Basic Syntax <?php
 
 class AutloadClass
 {
 public function loadMethod($className)
 {
 require_once(__DIR__ . ‘/../src/‘ . $className);
 }
 }
 
 spl_autoload_register([AutoloadClass::class, ‘loadMethod’]);
  44. 44. Basic Syntax <?php
 class Db
 {
 protected $connection;
 public function connect($db, $host, $port, $user, $pwd)
 {
 $this->connection = new PDO(
 ‘mysql:dbname=‘ . $db . ‘;host=‘ . $host .
 ‘;port=‘ . $port’,
 $user,
 $pwd
 );
 }
 }
  45. 45. Basic Syntax <?php
 
 // use frameworks for database
 
 $query = $this->getDb()->select()
 ->from([‘tn’ => ‘table_name’], [‘id’, ‘name’])
 ->join([‘ot’ => ‘other_table’], ‘ot.id = tn.other_id’, [‘other_thing’])
 ->where(‘tn.some_date > ?’, $date)
 ->where(‘ot.other_number = ?’, $number);
 
 $rows = $this->getDb()->fetchAll($query);
  46. 46. Basic Syntax <?php
 
 $hashed = password_hash($password, PASSWORD_DEFAULT);
 
 $isValid = password_verify($userEntered, $hashed);
  47. 47. Starting Out With PHP • What is PHP • Basic Syntax • Language Features • PHP Ecosystem
  48. 48. Language Features • Functional • Object-oriented
  49. 49. Language Features • Data types • Type juggling
  50. 50. Language Features • Method parameter type hinting • Method return type
  51. 51. Language Features • Class • Abstract • Interface • Trait
  52. 52. Language Features • Class - Constants - Properties - Methods - Magic methods
  53. 53. Language Features • Class - class Cat extends Pet implements HousePet {
 use MouseCatcherTrait;
 const FAMILY = ‘Feline’;
 protected $name;
 public function __construct($dependencies) {…}
 public static function independentAction() {…}
 protected function eatFood(Food $food) : Food {…}
 private function _controlHuman(Human $human) {…}
 }
  54. 54. Language Features • Abstract - Class can extend one abstract at a time - Can have parent, grandparent, etc. - Mixed functions filled in and others not - abstract class Pet {
 abstract protected function eatFood(Food $food) : Food {};
 }
  55. 55. Language Features • Interfaces - Contract - Class can implement many interfaces - interface Port {
 public function connect(Connection $connection);
 }
  56. 56. Language Features • Trait - Common characteristics - trait SomeCharacteristic {
 public function doSomething() {…}
 } - class Thing {
 use SomeCharacteristic;
 }
  57. 57. Language Features • Exceptions - try {
 …
 throw new Exception(‘Error message’);
 …
 throw new CustomException(‘Failed’);
 } catch (Exception | CustomException $e) {
 echo $e->getMessage();
 }
  58. 58. Language Features • Magic methods - Called automatically - __construct when instantiated - __set, __get - __destruct, __call, __callStatic, __isset, __unset, __sleep, __wakup, __toString, __invoke, __set_state, __clone, __debugInfo
  59. 59. Language Features • Namespaces - namespace AnimalMammalFeline;
 use AnimalFish;
 
 class Cat
 {
 public function eat(Fish $fish) {…}
 }
 …
 new AnimalMammalFelineCat;
  60. 60. Language Features • Libsodium - First programming language with modern security library built-in - Hash - Encrypt - Keys - Random
  61. 61. PHP Ecosystem • Frameworks • Tools • Community • Career
  62. 62. PHP Ecosystem • CMS Frameworks - Wordpress - Drupal - Joomla
  63. 63. PHP Ecosystem • Frameworks - larvel - Symfony - CakePHP - Zend - Slim
  64. 64. PHP Ecosystem • Frameworks - Framework Interop Group (PHP-FIG) ‣ Coding standard recommendations ‣ Common interfaces
  65. 65. PHP Ecosystem • Tools - composer package manager ‣ packagist repositories - PECL extension manager
  66. 66. PHP Ecosystem
  67. 67. PHP Ecosystem • Tools - PhpStorm - Zend Studio - Many other IDEs to choose from - Any text editor
  68. 68. PHP Ecosystem • Tools - PHPUnit - behat - phpspec - Faker
  69. 69. PHP Ecosystem • Tools - Xdebug ‣ Step-into debugging ‣ Stacktrace ‣ Performance
  70. 70. PHP Ecosystem • Tools - Guzzle - Apigility
  71. 71. PHP Ecosystem • Tools - git - Mercurial - Others
  72. 72. PHP Ecosystem • Community - User groups ‣ https://www.meetup.com/Utah-PHP-User-Group/ - Conferences - Magazine
  73. 73. PHP Ecosystem • Community - Slack channels ‣ utos.slack.com ‣ phpcommunity.slack.com ‣ phpchat.slack.com ‣ phpug.slack.com
  74. 74. PHP Ecosystem • Community - Open source - Diversity - Elephpants
  75. 75. PHP Ecosystem • Community - Welcoming - Open to other technologies, languages
  76. 76. PHP Ecosystem • Career - Experience - Education
  77. 77. PHP Ecosystem • Career - Lots of jobs ‣ Recruiters ‣ Job boards ‣ Contracts ‣ Who you know
  78. 78. PHP Ecosystem • Career - Good pay ‣ $40k-$60k starting ‣ $60k-$80k mid ‣ $80k+ advanced and lead
  79. 79. PHP Ecosystem • Career - Local - Remote
  80. 80. Starting Out With PHP • What is PHP • Basic Syntax • Language Features • PHP Ecosystem
  81. 81. Questions? • https://joind.in/talk/fa612
  82. 82. References • php.net

×