SlideShare a Scribd company logo
Mari Memahami PSR 
oleh 
Mizan Rizqia
Latar Belakang 
● PHP bahasa yg berkembang 
● PHP tidak standar 
● PHP tidak ada spesifikasi yg nyata
Latar Belakang 
PHP 5.4 
● OOP Lengkap 
● banyak fungsi baru
Bagaimana dgn Framework?
Masalah 
● Banyak PHP framework 
● Haruskah belajar semua PHP framework? 
● Apa framework yang bagus? 
● Apakah harus terpaku 1 PHP framework?
PHP-FIG 
PHP Framework Interop Group
Misi 
PHP-FIG 
Menemukan cara untuk membuat framework-framework 
PHP mampu bekerja bersama-sama
Anggota PHP-FIG 
● Agavi 
● AWS SDK for PHP (Amazon 
Web Services) 
● Apache log4php 
● Assetic and Buzz 
● Aura Project and Solar 
Framework 
● CakePHP 
● Composer and Packagist 
● Contao Open Source CMS 
● Doctrine 
● Drupal 
● eZ Publish 
● TYPO3 Flow 
● Jackalope 
● Joomla 
● Laravel 
● Lithium 
● PEAR 
● phpBB 
● phpDocumentor 
● PPI Framework 
● Propel 
● PyroCMS 
● SabreDAV 
● Sculpin 
● SugarCRM 
● Symfony2 
● Yii framework 
● Zend Framework 2 
● Zikula 
● lainnya….
Hasil PHP-FIG 
PHP Standards Recommendation 
(PSR)
PSR-0 
Autoloading Standard
PSR-0 Autoloading Standard 
Bagaimana semua framework dapat bekerja sama? 
● memiliki struktur direktori umum 
● memilik standar autoloading umum. 
JADI 
Semua framework harus dibangun dengan menggunakan 
konvensi autoloading yang sama.
PSR-0 Peraturan 
1. Struktur Fully-qualified Namespace dan Class harus mengikuti struktur 
<Vendor Name> (<Namespace>) * <Class Name> 
2. Setiap Namespace harus memiliki top-level namespace (“Nama Vendor”). 
3. Setiap Namespace dapat memiliki banyak sub-namespace sesuai 
kebutuhan. 
4. Setiap separator namespace diubah menjadi DIRECTORY_SEPARATOR 
ketika loading dari sistem file. 
5. Setiap karakter _ (Underscore) dalam CLASS NAME diubah menjadi 
DIRECTORY_SEPARATOR. Karakter _ (Underscore) tidak memiliki arti 
khusus dalam namespace. 
6. Fully-qualified Namespace dan Class berakhiran .php ketika loading dari 
sistem file. 
7. Urutan abjad dalam penamaan vendor, namespace, dan class merupakan 
kombinasi dari huruf kecil dan huruf besar.
PSR-0 Contoh 
● SymfonyCoreRequest => 
/path/to/project/lib/vendor/Symfony/Core/Request.php 
● ZendAcl => /path/to/project/lib/vendor/Zend/Acl.php 
● ZendMailMessage => 
/path/to/project/lib/vendor/Zend/Mail/Message.php
PSR-0 Contoh Implementasi 
● http://laravel.io/bin/yGE6D
PSR-1 
Basic Coding Standard
PSR-1 Basic Coding Standard 
Ini adalah tawaran PSR untuk standarisasi pengkodean 
PHP, dan idenya adalah jika semua pengembang 
menggunakan standar yang sama, maka akan mudah 
untuk memindahkan modul tanpa masalah.
PSR-1 Peraturan 
1. File HARUS menggunakan <?php dan <?= tags 
2. File HARUS menggunakan UTF-8 tanpa BOM untuk PHP Code. 
3. File SEBAIKNYA dapat mendeklarasikan simbol (classes, functions, 
constants, dll) atau yang menyebabkan efek samping (generate output, 
change .ini settings, dll) tapi HARUS TIDAK melakukan keduanya. 
4. Namespace dan Class HARUS mengikuti PSR-0. 
5. Nama Class HARUS dideklarasikan dalam StudlyCaps 
6. Constant kelas HARUS dinyatakan dalam huruf besar semua dengan 
pemisah underscore 
7. Nama Method HARUS dinyatakan dalam CamelCase
PSR-1 Files 
PHP Tags 
PHP code HARUS menggunakan 
<?php ?> atau short-echo <?= ?> 
Tidak boleh menggunakan variasi lainnya. 
Character Encoding 
Kode PHP HARUS menggunakan UTF-8 tanpa BOM
PSR-1 Files 
Side Effects 
Contoh penggunaan deklarasi dan side effects bersamaan 
yang harus dihindari 
http://laravel.io/bin/YydOn 
Contoh penggunaan deklarasi tanpa side effects 
http://laravel.io/bin/mGMlj
PSR-1 Namespace & Class Name 
● Namespace & Class Name HARUS mengikuti PSR-0 
● Class Name HARUS StudlyCaps 
Contoh Namespace dan Class Name 
http://laravel.io/bin/bEBGX
PSR-1 Class Constant, Properties, Methods 
Constant 
Class Constant: upper case with underscore seperator 
http://laravel.io/bin/Xy7Dj 
Properties 
Penamaan SEHARUSNYA konsisten sesuai scope. Contoh scope: vendor-level, 
package-level, class-level, or method-level. 
Methods 
Method names HARUS camelCase()
PSR-2 
Coding Style Guide
PSR-2 Coding Style Guide 
PSR ini penambahan dari PSR-1, dan 
seperangkat aturan mengenai bagaimana 
memformat kode PHP.
PSR-2 Peraturan 
1. Kode HARUS berdasarkan PSR-1. 
2. Kode harus menggunakan 4 spasi untuk indentasi, bukan tab. 
3. TIDAK HARUS ada batas pada panjang baris; batas soft HARUS 120 
karakter, setiap baris SEHARUSNYA 80 karakter atau kurang. 
4. HARUS ada satu baris kosong setelah deklarasi namespace, dan HARUS 
ada satu baris kosong setelah blok use deklarasi. 
5. Pembuka Brace ( Kurung kurawal ) untuk Class HARUS pada baris 
berikutnya, dan menutup Brace HARUS ada pada baris berikutnya setelah 
body class. 
6. Membuka Brace untuk method HARUS pada baris berikutnya, dan 
menutup kawat gigi HARUS pada baris berikutnya setelah body method.
PSR-2 Peraturan (lanjutan) 
7. Visibility HARUS dideklarasikan pada semua properti dan metode; abstract 
dan final harus dideklarasikan sebelum Visibility, static HARUS 
menyatakan setelah Visibility. 
8. Control struktur keyword HARUS memiliki satu spasi setelahnya; namun 
pada pemanggilan method dan function TIDAK HARUS. 
9. Membuka Brace untuk struktur Control HARUS pada baris yang sama, 
dan menutup kawat gigi HARUS pergi pada baris berikutnya setelah body 
control. 
10. Kurung buka untuk struktur kontrol TIDAK HARUS memiliki spasi setelah 
mereka, dan menutup kurung untuk struktur kontrol TIDAK HARUS 
memiliki spasi sebelumnya.
PSR-2 Contoh Implementasi 
http://laravel.io/bin/Mk5dJ
PSR-2 General 
Basic Coding Standard 
● Code HARUS mengikuti semua aturan PSR-1 
Files 
● Semua PHP files HARUS menggunakan Unix LF (linefeed) line ending 
● Semua PHP files HARUS berakhir dengan single blank line 
Lines 
● HARUS TIDAK terlalu panjang barisnya. 
● soft limit HARUS 120 karakter 
● SEBAIKNYA 80 karakter
PSR-2 General 
Indenting 
● Code HARUS menggunakan 4 space untuk indenting 
● Code HARUS TIDAK menggunaan tabs untuk indenting 
N.b.: Using only spaces, and not mixing spaces with tabs, helps to avoid 
problems with diffs, patches, history, and annotations. The use of spaces 
also makes it easy to insert fine-grained sub-indentation for inter-line 
alignment. 
Keywords and True/False/Null 
● PHP Keyword HARUS lower case 
● PHP constants true, false, null HARUS lower case
PSR-2 Namespace & Declarations 
● Setelah deklarasi namespace HARUS ada 1 baris kosong. 
● Semua deklarasi use HARUS berada di bawah deklarasi namespace 
● Deklarasi use HARUS untuk 1 keyword. 
● Setelah deklarasi blok use HARUS ada 1 baris kosong. 
Contoh 
http://laravel.io/bin/1y4ne
PSR-2 Classes, Properties, Methods 
Extends & Implements 
● keyword extends dan implements HARUS 
dideklarasikan dalam 1 baris 
Contoh 
http://laravel.io/bin/BLoka 
● Beberapa implements BOLEH dipisah menjadi 
beberapa baris, dimana setiap baris di-indent sekali. 
Contoh 
http://laravel.io/bin/2Wknr
PSR-2 Classes, Properties, Methods 
Properties 
● penglihatan/visibility HARUS dideklarasikan pada semua property 
● keyword var, HARUS TIDAK digunakan untuk mendeklarasikan property 
● HARUS TIDAK lebih dari 1 property per statement. 
● SEBAIKNYA TIDAK diberi awalan single underscore ( _ ) untuk 
mengindikasikan protected atau private visibility 
Contoh 
http://laravel.io/bin/VPzrn
PSR-2 Classes, Properties, Methods 
Methods 
● penglihatan/visibility HARUS dideklarasikan pada semua method 
● Nama Method SEBAIKNYA TIDAK diberi awalan single underscore ( _ ) 
untuk mengindikasikan protected atau private visibility 
● Nama Method HARUS TIDAK dideklarasikan dengan spasi setelah nama 
method. 
● Kurung buka dan kurung tutup HARUS berada dalam baris yang sama 
dengan nama method. 
● HARUS TIDAK boleh ada spasi setelah opening parenthesis dan sebelum 
closing parenthesis. 
● Contoh: http://laravel.io/bin/LkmGe
PSR-2 Classes, Properties, Methods 
Method Arguments 
● HARUS TIDAK ada space sebelum koma, dan HARUS ada space seteleh 
setiap koma. 
● Method arguments dengan nilai default HARUS berada dibelakang daftar 
argumen. 
Contoh: http://laravel.io/bin/wJnYx 
● Daftar argumen BOLEH dipisah menjadi beberapa baris, dimana setiap 
baris diberi 1 indent. 
Contoh: http://laravel.io/bin/qQElo
PSR-2 Classes, Properties, Methods 
abstract, final, and static 
● Deklarasi abstract dan final HARUS berada sebelum deklarasi 
visibility. 
● Deklarasi static HARUS berada setelah deklarasi visibility. 
● Contoh: http://laravel.io/bin/0e4yw
PSR-2 Classes, Properties, Methods 
Method & Function 
● HARUS TIDAK ada space antara nama method/function dengan kurung 
buka dan HARUS TIDAK ada space setelah kurung buka 
● HARUS TIDAK ada space sebelum kurung tutup dan HARUS TIDAK ada 
space setelah kurung tutup. 
Contoh: http://laravel.io/bin/7we1z 
● Daftar argumen BOLEH dipotong menjadi beberapa baris, dimana setiap 
baris diberi 1 indent. 
Contoh: http://laravel.io/bin/jQyP7
PSR-2 Control Structures 
● HARUS ada 1 spasi setelah control structures keyword 
● HARUS TIDAK ada spasi setelah opening parenthesis 
● HARUS TIDAK ada spasi sebelum closing parenthesis 
● HARUS ada spasi antara closing parenthesis dengan 
opening brace 
● Structured body HARUS ada 1 indent 
● Closing brace HARUS berada pada baris selanjutnya 
setelah body.
PSR-2 Control Structures 
● if, elseif, else 
contoh: http://laravel.io/bin/8Kq37 
● switch, case 
contoh: http://laravel.io/bin/aNB5P 
● while, do while 
contoh: http://laravel.io/bin/okY2w 
● for 
contoh: http://laravel.io/bin/vB4mV 
● foreach 
contoh: http://laravel.io/bin/yGEYR 
● try, catch 
contoh: http://laravel.io/bin/52YWn
PSR-2 Closures 
● Contoh deklarasi closure 
http://laravel.io/bin/kWxk0 
● Contoh deklarasi daftar argumen pada closure 
http://laravel.io/bin/l5vxV 
● Contoh deklarasi closure pada function or method call 
http://laravel.io/bin/W4Q95
> phpcs --standard=PSR2 *.php 
https://github.com/squizlabs/PHP_CodeSniffer
PSR-3 
Logger Interface
PSR-3 Logger Interface 
Peraturan PSR-3 untuk logging, dan secara khusus 
memaparkan sembilan metode untuk tata cara menulis log 
pada library framework. Hal ini membuat pemangkasan 
universal untuk semua kerangka, yang berarti akan 
mempermudah ketika ingin menerapkan logging dalam 
framework yang sebelumnya tidak mempunyai fungsi 
tersebut.
PSR-3 Cara 
1. LoggerInterface memperlihatkan delapan metode untuk menulis log ke 
dalam RFC 5424 (debug, info, notice, warning, error, critical, alert, 
emergency). 
2. Metode kesembilan (log), menerima tingkat log sebagai argumen pertama. 
Pemanggilan metode ini dengan salah satu dari tingkat konstanta log 
HARUS memiliki hasil yang sama seperti pemanggilan metode tingkat-spesifik. 
Pemanggilan metode ini dengan tidak disertai konstanta level oleh 
HARUS mengeluarkan PsrLogInvalidArgumentException jika 
pelaksanaannya tidak berhasil. Pengguna SEBAIKNYA TIDAK 
menggunakan tingkat log kustom tanpa mengetahui pasti cara 
pengimplementasiannya.
Mari Memahami PSR (PHP Standards Recommendation)
Mari Memahami PSR (PHP Standards Recommendation)
https://github.com/php-fig/log
{ 
“require”: { 
“psr/log”: “dev-master” 
} 
}
Monolog
PSR-4 
Improved Autoloading
PSR-4 Improved Autoloading 
Sebagai extention untuk PSR-0, PSR-4 
menambahkan metode peningkatan 
autoloading, dan juga menjelaskan persyaratan 
path file dari autoloader interoperable.
PSR-0 Way
PSR-4 Way
Referensi 
● http://idtut.com/2014/apa-itu-php-fig-dan-apa-yang-mereka-lakukan/ 
● http://www.php-fig.org/ 
● https://github.com/php-fig/fig-standards 
● http://www.sitepoint.com/autoloading-and-the-psr-0-standard/ 
● http://www.slideshare.net/arambaghdasaryan/coding-standards-psr1-psr2 
● http://www.slideshare.net/MinskPHPUserGroup/psr-36450518 
● http://www.slideshare.net/loalf/fig-and-psr
Matur Sembah Nuwun 
Thank you 
Mizan Rizqia 
baobaz@tlab.co.id 
081806406692 
@mizanrizqia 
/mizanr

More Related Content

PDF
Kernels and Support Vector Machines
PPTX
Multiple Classifier Systems
PPTX
Lecture 25 hill climbing
PPT
2.7 other classifiers
PDF
Gradient descent method
PPTX
Semantic Networks
PPTX
Activation Function.pptx
PPSX
Perceptron (neural network)
Kernels and Support Vector Machines
Multiple Classifier Systems
Lecture 25 hill climbing
2.7 other classifiers
Gradient descent method
Semantic Networks
Activation Function.pptx
Perceptron (neural network)

What's hot (20)

PPTX
Insertion sort algorithm power point presentation
PPTX
Learning sets of Rules by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
Exception handling c++
PPTX
Candidate elimination example
PDF
Informed search
PPTX
Convolutional Neural Network (CNN) - image recognition
PPT
Red black tree
PPTX
ML_ Unit 2_Part_B
PPT
Learning sets of rules, Sequential Learning Algorithm,FOIL
PPTX
Hill Climbing Algorithm in Artificial Intelligence
PPTX
Webinar usando graylog para la gestión centralizada de logs
PPTX
Alpha beta
PPTX
6-Python-Recursion PPT.pptx
PPTX
Presentation on Heap Sort
PDF
DTrace Topics: Introduction
PDF
pandas: Powerful data analysis tools for Python
PPTX
Deep deterministic policy gradient
PDF
Something About Dynamic Linking
PPTX
Uninformed Search technique
PPTX
Types of methods in python
Insertion sort algorithm power point presentation
Learning sets of Rules by Dr.C.R.Dhivyaa Kongu Engineering College
Exception handling c++
Candidate elimination example
Informed search
Convolutional Neural Network (CNN) - image recognition
Red black tree
ML_ Unit 2_Part_B
Learning sets of rules, Sequential Learning Algorithm,FOIL
Hill Climbing Algorithm in Artificial Intelligence
Webinar usando graylog para la gestión centralizada de logs
Alpha beta
6-Python-Recursion PPT.pptx
Presentation on Heap Sort
DTrace Topics: Introduction
pandas: Powerful data analysis tools for Python
Deep deterministic policy gradient
Something About Dynamic Linking
Uninformed Search technique
Types of methods in python
Ad

Viewers also liked (20)

PDF
Restful api design
PPT
PPT
Introduction to OOP with PHP
PDF
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3
PDF
Understanding REST-Based Services: Simple, Scalable, and Platform Independent
PDF
Tutorial dasar laravel
PDF
Nge-GIT (Belajar Git Bareng)
PDF
PHP CODING STANDARDS
PPT
Scalable Reliable Secure REST
ODP
OOP Basic - PHP
DOCX
Fungsi-Fungsi PHP
PDF
Coding standards PSR-1 & PSR-2
PDF
LinkedIn SlideShare: Knowledge, Well-Presented
PDF
What Makes Great Infographics
PDF
Masters of SlideShare
PDF
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
PDF
You Suck At PowerPoint!
PDF
10 Ways to Win at SlideShare SEO & Presentation Optimization
PDF
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
PDF
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
Restful api design
Introduction to OOP with PHP
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3
Understanding REST-Based Services: Simple, Scalable, and Platform Independent
Tutorial dasar laravel
Nge-GIT (Belajar Git Bareng)
PHP CODING STANDARDS
Scalable Reliable Secure REST
OOP Basic - PHP
Fungsi-Fungsi PHP
Coding standards PSR-1 & PSR-2
LinkedIn SlideShare: Knowledge, Well-Presented
What Makes Great Infographics
Masters of SlideShare
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
You Suck At PowerPoint!
10 Ways to Win at SlideShare SEO & Presentation Optimization
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
Ad

Similar to Mari Memahami PSR (PHP Standards Recommendation) (20)

PDF
PHP Tips & Trik Lanjutan: Clean Code dan Coding Standar
PPT
php ( hypertext markup language) bahasa pemrograman
PPT
3984793.ppt
PDF
Modul 4 php
PPT
Pengantar Pemrograman WEB PHP
PDF
Belajar php (pemula dasar)
PDF
Tutorial dasar laravel
PDF
Tutorial Penggunaan Laravel
PDF
Tutorial dasar-laravel
PDF
Tutorial dasar laravel 4
DOCX
Bpmogrman berorientasi fungsi
PPTX
Laravel Dasar.pptx
PPTX
Laravel Dasar.pptx
RTF
Pascal buku
PPT
Algoritma 02
PPTX
2024 - UNIKU - FKOM - TI - Ganjil 2024_2025 - BP1 - Teori - Materi - Pengena...
PPTX
belajar dasar dasar pemrogaraman Laravel Dasar.pptx
PPTX
Instalasi dan Konfigurasi Laravel Versi 5 di Windows.pptx
PDF
Meeting 9 pemrograman shell - a
DOCX
Flowchart
PHP Tips & Trik Lanjutan: Clean Code dan Coding Standar
php ( hypertext markup language) bahasa pemrograman
3984793.ppt
Modul 4 php
Pengantar Pemrograman WEB PHP
Belajar php (pemula dasar)
Tutorial dasar laravel
Tutorial Penggunaan Laravel
Tutorial dasar-laravel
Tutorial dasar laravel 4
Bpmogrman berorientasi fungsi
Laravel Dasar.pptx
Laravel Dasar.pptx
Pascal buku
Algoritma 02
2024 - UNIKU - FKOM - TI - Ganjil 2024_2025 - BP1 - Teori - Materi - Pengena...
belajar dasar dasar pemrogaraman Laravel Dasar.pptx
Instalasi dan Konfigurasi Laravel Versi 5 di Windows.pptx
Meeting 9 pemrograman shell - a
Flowchart

Mari Memahami PSR (PHP Standards Recommendation)

  • 1. Mari Memahami PSR oleh Mizan Rizqia
  • 2. Latar Belakang ● PHP bahasa yg berkembang ● PHP tidak standar ● PHP tidak ada spesifikasi yg nyata
  • 3. Latar Belakang PHP 5.4 ● OOP Lengkap ● banyak fungsi baru
  • 5. Masalah ● Banyak PHP framework ● Haruskah belajar semua PHP framework? ● Apa framework yang bagus? ● Apakah harus terpaku 1 PHP framework?
  • 6. PHP-FIG PHP Framework Interop Group
  • 7. Misi PHP-FIG Menemukan cara untuk membuat framework-framework PHP mampu bekerja bersama-sama
  • 8. Anggota PHP-FIG ● Agavi ● AWS SDK for PHP (Amazon Web Services) ● Apache log4php ● Assetic and Buzz ● Aura Project and Solar Framework ● CakePHP ● Composer and Packagist ● Contao Open Source CMS ● Doctrine ● Drupal ● eZ Publish ● TYPO3 Flow ● Jackalope ● Joomla ● Laravel ● Lithium ● PEAR ● phpBB ● phpDocumentor ● PPI Framework ● Propel ● PyroCMS ● SabreDAV ● Sculpin ● SugarCRM ● Symfony2 ● Yii framework ● Zend Framework 2 ● Zikula ● lainnya….
  • 9. Hasil PHP-FIG PHP Standards Recommendation (PSR)
  • 11. PSR-0 Autoloading Standard Bagaimana semua framework dapat bekerja sama? ● memiliki struktur direktori umum ● memilik standar autoloading umum. JADI Semua framework harus dibangun dengan menggunakan konvensi autoloading yang sama.
  • 12. PSR-0 Peraturan 1. Struktur Fully-qualified Namespace dan Class harus mengikuti struktur <Vendor Name> (<Namespace>) * <Class Name> 2. Setiap Namespace harus memiliki top-level namespace (“Nama Vendor”). 3. Setiap Namespace dapat memiliki banyak sub-namespace sesuai kebutuhan. 4. Setiap separator namespace diubah menjadi DIRECTORY_SEPARATOR ketika loading dari sistem file. 5. Setiap karakter _ (Underscore) dalam CLASS NAME diubah menjadi DIRECTORY_SEPARATOR. Karakter _ (Underscore) tidak memiliki arti khusus dalam namespace. 6. Fully-qualified Namespace dan Class berakhiran .php ketika loading dari sistem file. 7. Urutan abjad dalam penamaan vendor, namespace, dan class merupakan kombinasi dari huruf kecil dan huruf besar.
  • 13. PSR-0 Contoh ● SymfonyCoreRequest => /path/to/project/lib/vendor/Symfony/Core/Request.php ● ZendAcl => /path/to/project/lib/vendor/Zend/Acl.php ● ZendMailMessage => /path/to/project/lib/vendor/Zend/Mail/Message.php
  • 14. PSR-0 Contoh Implementasi ● http://laravel.io/bin/yGE6D
  • 15. PSR-1 Basic Coding Standard
  • 16. PSR-1 Basic Coding Standard Ini adalah tawaran PSR untuk standarisasi pengkodean PHP, dan idenya adalah jika semua pengembang menggunakan standar yang sama, maka akan mudah untuk memindahkan modul tanpa masalah.
  • 17. PSR-1 Peraturan 1. File HARUS menggunakan <?php dan <?= tags 2. File HARUS menggunakan UTF-8 tanpa BOM untuk PHP Code. 3. File SEBAIKNYA dapat mendeklarasikan simbol (classes, functions, constants, dll) atau yang menyebabkan efek samping (generate output, change .ini settings, dll) tapi HARUS TIDAK melakukan keduanya. 4. Namespace dan Class HARUS mengikuti PSR-0. 5. Nama Class HARUS dideklarasikan dalam StudlyCaps 6. Constant kelas HARUS dinyatakan dalam huruf besar semua dengan pemisah underscore 7. Nama Method HARUS dinyatakan dalam CamelCase
  • 18. PSR-1 Files PHP Tags PHP code HARUS menggunakan <?php ?> atau short-echo <?= ?> Tidak boleh menggunakan variasi lainnya. Character Encoding Kode PHP HARUS menggunakan UTF-8 tanpa BOM
  • 19. PSR-1 Files Side Effects Contoh penggunaan deklarasi dan side effects bersamaan yang harus dihindari http://laravel.io/bin/YydOn Contoh penggunaan deklarasi tanpa side effects http://laravel.io/bin/mGMlj
  • 20. PSR-1 Namespace & Class Name ● Namespace & Class Name HARUS mengikuti PSR-0 ● Class Name HARUS StudlyCaps Contoh Namespace dan Class Name http://laravel.io/bin/bEBGX
  • 21. PSR-1 Class Constant, Properties, Methods Constant Class Constant: upper case with underscore seperator http://laravel.io/bin/Xy7Dj Properties Penamaan SEHARUSNYA konsisten sesuai scope. Contoh scope: vendor-level, package-level, class-level, or method-level. Methods Method names HARUS camelCase()
  • 23. PSR-2 Coding Style Guide PSR ini penambahan dari PSR-1, dan seperangkat aturan mengenai bagaimana memformat kode PHP.
  • 24. PSR-2 Peraturan 1. Kode HARUS berdasarkan PSR-1. 2. Kode harus menggunakan 4 spasi untuk indentasi, bukan tab. 3. TIDAK HARUS ada batas pada panjang baris; batas soft HARUS 120 karakter, setiap baris SEHARUSNYA 80 karakter atau kurang. 4. HARUS ada satu baris kosong setelah deklarasi namespace, dan HARUS ada satu baris kosong setelah blok use deklarasi. 5. Pembuka Brace ( Kurung kurawal ) untuk Class HARUS pada baris berikutnya, dan menutup Brace HARUS ada pada baris berikutnya setelah body class. 6. Membuka Brace untuk method HARUS pada baris berikutnya, dan menutup kawat gigi HARUS pada baris berikutnya setelah body method.
  • 25. PSR-2 Peraturan (lanjutan) 7. Visibility HARUS dideklarasikan pada semua properti dan metode; abstract dan final harus dideklarasikan sebelum Visibility, static HARUS menyatakan setelah Visibility. 8. Control struktur keyword HARUS memiliki satu spasi setelahnya; namun pada pemanggilan method dan function TIDAK HARUS. 9. Membuka Brace untuk struktur Control HARUS pada baris yang sama, dan menutup kawat gigi HARUS pergi pada baris berikutnya setelah body control. 10. Kurung buka untuk struktur kontrol TIDAK HARUS memiliki spasi setelah mereka, dan menutup kurung untuk struktur kontrol TIDAK HARUS memiliki spasi sebelumnya.
  • 26. PSR-2 Contoh Implementasi http://laravel.io/bin/Mk5dJ
  • 27. PSR-2 General Basic Coding Standard ● Code HARUS mengikuti semua aturan PSR-1 Files ● Semua PHP files HARUS menggunakan Unix LF (linefeed) line ending ● Semua PHP files HARUS berakhir dengan single blank line Lines ● HARUS TIDAK terlalu panjang barisnya. ● soft limit HARUS 120 karakter ● SEBAIKNYA 80 karakter
  • 28. PSR-2 General Indenting ● Code HARUS menggunakan 4 space untuk indenting ● Code HARUS TIDAK menggunaan tabs untuk indenting N.b.: Using only spaces, and not mixing spaces with tabs, helps to avoid problems with diffs, patches, history, and annotations. The use of spaces also makes it easy to insert fine-grained sub-indentation for inter-line alignment. Keywords and True/False/Null ● PHP Keyword HARUS lower case ● PHP constants true, false, null HARUS lower case
  • 29. PSR-2 Namespace & Declarations ● Setelah deklarasi namespace HARUS ada 1 baris kosong. ● Semua deklarasi use HARUS berada di bawah deklarasi namespace ● Deklarasi use HARUS untuk 1 keyword. ● Setelah deklarasi blok use HARUS ada 1 baris kosong. Contoh http://laravel.io/bin/1y4ne
  • 30. PSR-2 Classes, Properties, Methods Extends & Implements ● keyword extends dan implements HARUS dideklarasikan dalam 1 baris Contoh http://laravel.io/bin/BLoka ● Beberapa implements BOLEH dipisah menjadi beberapa baris, dimana setiap baris di-indent sekali. Contoh http://laravel.io/bin/2Wknr
  • 31. PSR-2 Classes, Properties, Methods Properties ● penglihatan/visibility HARUS dideklarasikan pada semua property ● keyword var, HARUS TIDAK digunakan untuk mendeklarasikan property ● HARUS TIDAK lebih dari 1 property per statement. ● SEBAIKNYA TIDAK diberi awalan single underscore ( _ ) untuk mengindikasikan protected atau private visibility Contoh http://laravel.io/bin/VPzrn
  • 32. PSR-2 Classes, Properties, Methods Methods ● penglihatan/visibility HARUS dideklarasikan pada semua method ● Nama Method SEBAIKNYA TIDAK diberi awalan single underscore ( _ ) untuk mengindikasikan protected atau private visibility ● Nama Method HARUS TIDAK dideklarasikan dengan spasi setelah nama method. ● Kurung buka dan kurung tutup HARUS berada dalam baris yang sama dengan nama method. ● HARUS TIDAK boleh ada spasi setelah opening parenthesis dan sebelum closing parenthesis. ● Contoh: http://laravel.io/bin/LkmGe
  • 33. PSR-2 Classes, Properties, Methods Method Arguments ● HARUS TIDAK ada space sebelum koma, dan HARUS ada space seteleh setiap koma. ● Method arguments dengan nilai default HARUS berada dibelakang daftar argumen. Contoh: http://laravel.io/bin/wJnYx ● Daftar argumen BOLEH dipisah menjadi beberapa baris, dimana setiap baris diberi 1 indent. Contoh: http://laravel.io/bin/qQElo
  • 34. PSR-2 Classes, Properties, Methods abstract, final, and static ● Deklarasi abstract dan final HARUS berada sebelum deklarasi visibility. ● Deklarasi static HARUS berada setelah deklarasi visibility. ● Contoh: http://laravel.io/bin/0e4yw
  • 35. PSR-2 Classes, Properties, Methods Method & Function ● HARUS TIDAK ada space antara nama method/function dengan kurung buka dan HARUS TIDAK ada space setelah kurung buka ● HARUS TIDAK ada space sebelum kurung tutup dan HARUS TIDAK ada space setelah kurung tutup. Contoh: http://laravel.io/bin/7we1z ● Daftar argumen BOLEH dipotong menjadi beberapa baris, dimana setiap baris diberi 1 indent. Contoh: http://laravel.io/bin/jQyP7
  • 36. PSR-2 Control Structures ● HARUS ada 1 spasi setelah control structures keyword ● HARUS TIDAK ada spasi setelah opening parenthesis ● HARUS TIDAK ada spasi sebelum closing parenthesis ● HARUS ada spasi antara closing parenthesis dengan opening brace ● Structured body HARUS ada 1 indent ● Closing brace HARUS berada pada baris selanjutnya setelah body.
  • 37. PSR-2 Control Structures ● if, elseif, else contoh: http://laravel.io/bin/8Kq37 ● switch, case contoh: http://laravel.io/bin/aNB5P ● while, do while contoh: http://laravel.io/bin/okY2w ● for contoh: http://laravel.io/bin/vB4mV ● foreach contoh: http://laravel.io/bin/yGEYR ● try, catch contoh: http://laravel.io/bin/52YWn
  • 38. PSR-2 Closures ● Contoh deklarasi closure http://laravel.io/bin/kWxk0 ● Contoh deklarasi daftar argumen pada closure http://laravel.io/bin/l5vxV ● Contoh deklarasi closure pada function or method call http://laravel.io/bin/W4Q95
  • 39. > phpcs --standard=PSR2 *.php https://github.com/squizlabs/PHP_CodeSniffer
  • 41. PSR-3 Logger Interface Peraturan PSR-3 untuk logging, dan secara khusus memaparkan sembilan metode untuk tata cara menulis log pada library framework. Hal ini membuat pemangkasan universal untuk semua kerangka, yang berarti akan mempermudah ketika ingin menerapkan logging dalam framework yang sebelumnya tidak mempunyai fungsi tersebut.
  • 42. PSR-3 Cara 1. LoggerInterface memperlihatkan delapan metode untuk menulis log ke dalam RFC 5424 (debug, info, notice, warning, error, critical, alert, emergency). 2. Metode kesembilan (log), menerima tingkat log sebagai argumen pertama. Pemanggilan metode ini dengan salah satu dari tingkat konstanta log HARUS memiliki hasil yang sama seperti pemanggilan metode tingkat-spesifik. Pemanggilan metode ini dengan tidak disertai konstanta level oleh HARUS mengeluarkan PsrLogInvalidArgumentException jika pelaksanaannya tidak berhasil. Pengguna SEBAIKNYA TIDAK menggunakan tingkat log kustom tanpa mengetahui pasti cara pengimplementasiannya.
  • 46. { “require”: { “psr/log”: “dev-master” } }
  • 49. PSR-4 Improved Autoloading Sebagai extention untuk PSR-0, PSR-4 menambahkan metode peningkatan autoloading, dan juga menjelaskan persyaratan path file dari autoloader interoperable.
  • 52. Referensi ● http://idtut.com/2014/apa-itu-php-fig-dan-apa-yang-mereka-lakukan/ ● http://www.php-fig.org/ ● https://github.com/php-fig/fig-standards ● http://www.sitepoint.com/autoloading-and-the-psr-0-standard/ ● http://www.slideshare.net/arambaghdasaryan/coding-standards-psr1-psr2 ● http://www.slideshare.net/MinskPHPUserGroup/psr-36450518 ● http://www.slideshare.net/loalf/fig-and-psr
  • 53. Matur Sembah Nuwun Thank you Mizan Rizqia [email protected] 081806406692 @mizanrizqia /mizanr