The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.
PSR-0: Autoloading Standard
In the beginning
<?php // login.php
$conn = new mysqli('localhost', 'username', 'password'); // Database connection
if (!$conn) { die('Connection failed: ' . mysqli_connect_error()); }
if (isset($_POST['submit'])) { // Check form submit
$user = $_POST['user'];
$pass = $_POST['pass'];
$result = $conn->query(
"SELECT * FROM user WHERE username='{$user}' AND password='{$pass}'"
);
header('Location:' . (mysqli_num_rows($result) > 0 ? 'ok.php' : 'fail.php'));
exit;
}
?>
<form method="POST">
Username: <input type="text" name="user"><br />
Password: <input type="password" name="pass"><br />
<input type="submit" />
</form>
Files MUST use only UTF-8 without BOM for PHP code.
Files SHOULD either declare symbols or cause side-effects but SHOULD NOT do both.
<?php // example to avoid
ini_set('error_reporting', E_ALL); // side effect: change ini settings
include "file.php"; // side effect: loads a file
echo "<html>\n"; // side effect: generates output
// declaration
function foo()
{
// function body
}
Namespaces and Class Names
Namespaces and classes MUST follow an "autoloading" PSR: [PSR-0, PSR-4].
Class names MUST be declared in
StudlyCaps
.
<?php // PHP 5.3 and later
namespace Vendor\Model;
class Foo
{
}
<?php // PHP 5.2.x and earlier
class Vendor_Model_Foo
{
}
Class Constants, Properties, and Methods
Class constants MUST be declared in all upper case with underscore separators.
Method names MUST be declared in
camelCase
.
<?php
namespace Vendor\Model;
class Foo
{
const VERSION = '1.0';
const DATE_APPROVED = '2012-06-01';
public function myMethodName()
{
// method body
}
}
<?php
namespace Vendor\Package;
use FooInterface;
use OtherVendor\OtherPackage\BazClass as Bar;
class Foo extends Bar implements FooInterface
{
public function sampleFunction($a, $b = null)
{
if ($a === $b) {
bar();
} elseif ($a > $b) {
$foo->bar($arg1);
} else {
BazClass::bar($arg2, $arg3);
}
}
final public static function bar()
{
}
}
$queryParams = $request->getQueryParams();
$parsedBody = $request->getParsedBody(); // deserialized body params
$uploadedFiles = $request->getUploadedFiles(); // array of UploadedFileInterface
// Get `id` passed from route, eg. localhost/user/100.
// Application must parse route and inject into request attributes
$id = $request->getAttribute('id'); // 100
StreamInterface
interface StreamInterface
{
public function __toString(); // important
public function close();
public function detach();
public function getSize();
public function tell();
public function eof();
public function isSeekable();
public function seek($offset, $whence = SEEK_SET);
public function rewind();
public function isWritable();
public function write($string);
public function isReadable();
public function read($length);
public function getContents();
public function getMetadata($key = null);
}
authority path
┌───────────────┴───────────────┐┌───┴────┐
http://username:password@example.com:123/path/data?key=value&key2=value2#fragid1
└─┬┘ └───────┬───────┘ └────┬────┘ └┬┘ └─────────┬─────────┘ └──┬──┘
scheme user information host port query fragment