PSR-1 – Önerilen PHP Standartları
Merhabalar,
Daha önce giriş yaptığımız ve autoloading üzerine standart olan PSR-0‘a giriş yapmıştık. PSR-1 ise Kod standartları üzerinedir.
Zorunluluklar
– PSR-0 içerisindeki tüm tavsiyeler geçerlidir.
– Kodlar – Dosyalar UTF-8 Bom’suz olmalıdır
– Declare veya overwrite söz konusu olduğunda yorum olarak belirtilmesi
– Namespace ve Class isimleri için PSR-0 standartları geçerli
– Class isimleri StudlyCaps şeklinde olmalı
– Class içerisindeki sabit değerler (const) büüyk harf olmalı, istenirse “_” içerebilir
– Method isimleri camelCase olmalı Örneğin; indexAction
– Değişken isimlerinde StudlyCaps, camelCase veya hepsi küçük şekilde alt çizgi dahil kullanım olabilir. Örneğin: toDay, to_day
Örnekler;
Side Effects
<?php
// side effect: change ini settings
ini_set('error_reporting', E_ALL);
// side effect: loads a file
include "file.php";
// side effect: generates output
echo "<html>\n";
// declaration
function foo()
{
// function body
}
<?php
// declaration
function foo()
{
// function body
}
// conditional declaration is *not* a side effect
if (! function_exists('bar')) {
function bar()
{
// function body
}
}
Namespace ve Class isimleri
<?php
// PHP 5.3 and later:
namespace Vendor\Model;
class Foo
{
}
<?php
// PHP 5.2.x and earlier:
class Vendor_Model_Foo
{
}
Constant isimleri ;
<?php
namespace Vendor\Model;
class Foo
{
const VERSION = '1.0';
const DATE_APPROVED = '2012-06-01';
}