{"id":433,"date":"2016-10-07T20:56:55","date_gmt":"2016-10-07T17:56:55","guid":{"rendered":"https:\/\/burakcaliskan.org\/blog\/?p=433"},"modified":"2016-10-18T10:39:30","modified_gmt":"2016-10-18T07:39:30","slug":"php-kodlama-standartlari","status":"publish","type":"post","link":"https:\/\/burakcaliskan.org\/blog\/php-kodlama-standartlari\/","title":{"rendered":"PHP Kodlama Standartlar\u0131"},"content":{"rendered":"<p>PHP dil olarak herhangi bir yaz\u0131m standard\u0131na sahip de\u011fildir, bu nedenle projelerde yada k\u00fct\u00fcphanelerde de\u011fi\u015fik yaz\u0131m stilleri g\u00f6rebilirsiniz. Bu durum hem bizim ba\u015fka kodlar\u0131 okumam\u0131z\u0131 zorla\u015ft\u0131r\u0131r, hemde bizden sonra gelen yaz\u0131l\u0131m geli\u015ftiricilerinin bizim kodlar\u0131m\u0131z\u0131 okumas\u0131 zorla\u015f\u0131r. Yani k\u0131saca her y\u00f6nden zararl\u0131 \u00e7\u0131kan bizler oluyoruz. Bu sebeple d\u00fcnyaca geli\u015ftiricilerinin belirledikleri PSR standartlar\u0131 bulunmaktad\u0131r. Bu standartlar b\u00fcy\u00fck projelerde, frameworklerde g\u00f6rmeniz m\u00fcmk\u00fcn. Hal b\u00f6yle olunca bizimde bu standartlar\u0131 \u00f6\u011frenmemiz iyi, kullanmam\u0131z \u00e7ok \u00e7ok iyi olacakt\u0131r.<\/p>\n<p><!--more--><\/p>\n<p><strong>PSR-0<\/strong><\/p>\n<p>Autoloading yap\u0131s\u0131 \u00fczerine kurulu bir mimariye sahiptir.<br \/>\nHer s\u0131n\u0131f\u0131n bir namespace\u2019i olmal\u0131d\u0131r.<br \/>\nHer namespace\u2019in bir \u00fcst namespace\u2019i olmal\u0131d\u0131r. Yani; \\Proje_adi\\namespace\\class\\<br \/>\nHer namespace\u2019in alt namespace\u2019leri olabilir.<br \/>\nHer namespace \u201c_\u201d i\u015fareti, \/ (DIRECTORY_SEPARATOR) olarak alg\u0131lanmal\u0131d\u0131r.<br \/>\nProje ad\u0131nda, s\u0131n\u0131f isimlerinde b\u00fcy\u00fck k\u00fc\u00e7\u00fck harf kombinasyonlar\u0131 olabilir.<br \/>\nPSR-0 \u00d6rnekleri:<\/p>\n<pre class=\"lang:default decode:true\">\\Doctrine\\Common\\IsolatedClassLoader =&gt;\/path\/to\/project\/lib\/vendor\/Doctrine\/Common\/IsolatedClassLoader.php\r\n\\Symfony\\Core\\Request =&gt; \/path\/to\/project\/lib\/vendor\/Symfony\/Core\/Request.php\r\n\\Zend\\Acl =&gt; \/path\/to\/project\/lib\/vendor\/Zend\/Acl.php\r\n\\Zend\\Mail\\Message =&gt; \/path\/to\/project\/lib\/vendor\/Zend\/Mail\/Message.php<\/pre>\n<p><strong> PSR-1<\/strong><\/p>\n<p>PSR-1 standartlar\u0131 PSR-0 standartlar\u0131n\u0131 aynen kabul eder ve bu standartta genel kod yaz\u0131m stili \u00fczerinde kurallar verir.<br \/>\nPHP dosyalar\u0131 <!--?php ba\u015flamal\u0131.&lt;br ?--> Dosyalar UTF-8 ve BOM\u2019suz olmal\u0131d\u0131r.<br \/>\nS\u0131n\u0131f isimleri StudlyCaps olmal\u0131d\u0131r.<br \/>\nS\u0131n\u0131f sabitleri tamam\u0131 b\u00fcy\u00fck harften olu\u015fmal\u0131d\u0131r.<br \/>\nMetot isimleri CamelCase olmal\u0131d\u0131r.<br \/>\nDe\u011fi\u015fken isimlerinde StudlyCaps, camelCase veya hepsi k\u00fc\u00e7\u00fck \u015fekilde alt \u00e7izgi dahil kullan\u0131m olabilir. getOption, get_option<br \/>\n<strong>PSR-1 \u00d6rnek<\/strong><br \/>\n<!--?php &lt;br ?--><\/p>\n<pre class=\"lang:default decode:true\">\/\/ side effect: change ini settings\r\nini_set('error_reporting', E_ALL);\r\n\r\n\/\/ PHP 5.3 and later:\r\nnamespace ProjeAdi\\Model;\r\n\r\nclass Foo\r\n{\r\n}\r\n\r\n\/\/ PHP 5.2.x and earlier:\r\nclass ProjeAdi_Model_Foo\r\n{\r\n}\r\n\r\nclass Foo\r\n{\r\nconst VERSION = '1.0';\r\nconst DATE_APPROVED = '2012-06-01';\r\n}<\/pre>\n<p><strong>PSR-2 Standatlar\u0131<\/strong><\/p>\n<p>PSR-1\u2019i destekler.<br \/>\nHer sat\u0131rda \u00f6nerilen 80 karakter, max 120 karakter olmal\u0131d\u0131r.<br \/>\nSat\u0131rlarda tab yerine 4 bo\u015fluk kullan\u0131lmal\u0131d\u0131r. (whitespace).<br \/>\nNamespace, class ismi, methot isminden sonra 1 bo\u015fluk b\u0131rak\u0131lmal\u0131.<br \/>\nMetot, s\u0131n\u0131f olu\u015fturuldu\u011funda a\u00e7\u0131lan s\u00fcsl\u00fc parantezler \u201c{\u201d ismin biti\u015finde de\u011fil bir alt sat\u0131rda a\u00e7\u0131lmal\u0131.<br \/>\nOperat\u00f6rler ile de\u011fi\u015fkenler aras\u0131nda bir karakter bo\u015fluk b\u0131rak\u0131lmal\u0131.<br \/>\ntrue,false,null k\u00fc\u00e7\u00fck kullan\u0131lmal\u0131.<br \/>\nPSR-2 \u00d6rnek:<\/p>\n<p><!--?php namespace Vendor\\Package; use FooInterface; use BarClass as Bar; use OtherVendor\\OtherPackage\\BazClass; class Foo extends Bar implements FooInterface { public function sampleFunction($a, $b = null) { if ($a === $b) { bar(); } elseif ($a &gt; $b) {&lt;br ?--><\/p>\n<pre class=\"lang:default decode:true \">$foo-&gt;bar($arg1);\r\n} else {\r\nBazClass::bar($arg2, $arg3);\r\n}\r\n}\r\n\r\nfinal public static function bar()\r\n{\r\n\/\/ method body\r\n}\r\n}<\/pre>\n<p><strong>PSR-3 Standartlar\u0131<\/strong><\/p>\n<p>Loglama \u00fczerine kurulu standartlard\u0131r.<br \/>\n8 level log tipi olmas\u0131n\u0131 \u00f6neriyor. (debug, info, notice, warning, error, critical, alert, emergency)<br \/>\nHer metot string veya obje kabul edebilir.<br \/>\nMesaj i\u00e7erisindeki placeholder lar, verilen array i\u00e7erisindekiler ile de\u011fi\u015ftirilir.<br \/>\nMesaj i\u00e7erisindeki placeholder lar {} aras\u0131nda yaz\u0131l\u0131r.<br \/>\nPlaceholder lar A-Z, a-z, 0-9, _ karakterlerinden olu\u015fabilir.<br \/>\n<strong>PSR-3 \u00d6rnek<\/strong><\/p>\n<p><!--?php \/** * Interpolates context values into the message placeholders. *\/ function interpolate($message, array $context = array()) { \/\/ build a replacement array with braces around the context keys $replace = array(); foreach ($context as $key =&gt; $val) {&lt;br ?--><\/p>\n<pre class=\"lang:default decode:true \">$replace['{' . $key . '}'] = $val;\r\n}\r\n\r\n\/\/ interpolate replacement values into the message and return\r\nreturn strtr($message, $replace);\r\n}\r\n\r\n\/\/ a message with brace-delimited placeholder names\r\n$message = \"User {username} created\";\r\n\r\n\/\/ a context array of placeholder names =&gt; replacement values\r\n$context = array('username' =&gt; 'bolivar');\r\n\r\n\/\/ echoes \"User bolivar created\"\r\necho interpolate($message, $context);<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP dil olarak herhangi bir yaz\u0131m standard\u0131na sahip de\u011fildir, bu nedenle projelerde yada k\u00fct\u00fcphanelerde de\u011fi\u015fik yaz\u0131m stilleri g\u00f6rebilirsiniz. Bu durum hem bizim ba\u015fka kodlar\u0131 okumam\u0131z\u0131 zorla\u015ft\u0131r\u0131r, hemde bizden sonra gelen yaz\u0131l\u0131m geli\u015ftiricilerinin bizim kodlar\u0131m\u0131z\u0131 okumas\u0131 zorla\u015f\u0131r. Yani k\u0131saca her y\u00f6nden zararl\u0131 \u00e7\u0131kan bizler oluyoruz. Bu sebeple d\u00fcnyaca geli\u015ftiricilerinin belirledikleri PSR standartlar\u0131 bulunmaktad\u0131r. Bu standartlar b\u00fcy\u00fck [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":83,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[25,264,267,266,265],"_links":{"self":[{"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/posts\/433"}],"collection":[{"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/comments?post=433"}],"version-history":[{"count":6,"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/posts\/433\/revisions"}],"predecessor-version":[{"id":459,"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/posts\/433\/revisions\/459"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/media\/83"}],"wp:attachment":[{"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/media?parent=433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/categories?post=433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/tags?post=433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}