programming best practices (php)
TRANSCRIPT
![Page 1: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/1.jpg)
![Page 2: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/2.jpg)
What will you be able to do?Goal: Write safer, faster and more robust PHP code
![Page 3: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/3.jpg)
Agenda1. Coding Style2. Coding Practices3. Security4. Errors and Exceptions
![Page 4: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/4.jpg)
Coding Style - Namespaces
• Huge PHP community = lots of code• If 2 libraries share same class name = oh oh, troubles…• Namespaces are similar to “OS directories”,
– 2 files with same name can co-exist in separate directories
– 2 classes with same name can co-exist in separate PHP namespaces
![Page 5: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/5.jpg)
Coding Style - Namespaces - Ex
Code example
![Page 6: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/6.jpg)
Coding Style - PSR-X
• PSR = PHP Standards Recommendations
• Coding style followed by most frameworks and latest libraries.
• Ex PSR-2 https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md
![Page 7: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/7.jpg)
Coding Practices
• DRY code ( Don’t Repeat Yourself)– Create functions and reuse them whenever you have code very similar
in different parts of your applications
• Use Object Oriented Programming whenever possible– Create classes to run your business logic (1 class per file if possible)
![Page 8: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/8.jpg)
Coding Practices
• Separate template code from business logic– Your template files should only load and display data, no processing– Your class files should not start/run themselves and should not directly
display content (your methods can)
![Page 9: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/9.jpg)
Security
• Input Validation
• SQL Injection
![Page 10: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/10.jpg)
Security - Input Validation
• Never trust user input• Always test if an input or variable exist
– isset($variable) = test if variable is not NULL– empty($array) = test if array is empty
• Then filter that input to make sure you receive what you expected– filter_var (for any kind of variables)– filter_input (for form inputs)
![Page 11: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/11.jpg)
Security - Input Validation
![Page 12: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/12.jpg)
Security - Input Validation
![Page 13: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/13.jpg)
Security - SQL Injection
![Page 14: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/14.jpg)
Security - SQL Injection
• Use PDO prepare statements (Laravel Eloquent uses it by default and Wordpress has its own prepare statements
![Page 15: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/15.jpg)
Errors & Exceptions - Errors
• PHP is an “exception-light” programming language.
• Unless a “fatal error” occurs, most of PHP will try to keep processing
• 3 types of error severity:• E_ERROR,E_NOTICE, and E_WARNING
![Page 16: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/16.jpg)
Errors & Exception - ErrorException Class
• Throw your “errors” as “exceptions” using the ErrorException class, which extends the Exception class.
• Common practice for several frameworks (Laravel, Symphony, etc.)
• Allow to handle errors better than the usual result by “catching” exceptions
![Page 17: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/17.jpg)
Errors & Exceptions - Exceptions
• Exceptions are often overlooked by PHP programmers.
• Some old PHP frameworks returns “false” or “Warning” when something goes wrong.– You have to dig in the doc and reread the code to
find what’s wrong
![Page 18: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/18.jpg)
Errors & Exceptions - Exceptions
![Page 19: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/19.jpg)
You Do
Each of you will apply the techniques we learnedtoday to optimize/secure/clean your previousprojects.Make sure to version your previous code before making any changes so I can see the progression
![Page 20: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/20.jpg)
Resources
• http://www.phptherightway.com/
![Page 21: Programming best practices (PHP)](https://reader035.vdocuments.site/reader035/viewer/2022062412/5879f5ed1a28ab70298b5985/html5/thumbnails/21.jpg)