DEV Community

Ketan Baitule
Ketan Baitule

Posted on

Writing PHP Code like a Pro: A Guide to PSR-1(Part 1)

Hi Developers,

When you're in the world of PHP, you want to follow some rules to keep your code clean and compatible with others. That's where the PHP-FIG (PHP Framework Interoperability Group) steps in with a set of rules known as PSRs (PHP Standards Recommendations). PSR-1 is one of the fundamental ones, and today, we're going to break it down for you.

1. File

1.1 PHP-Tags

Okay, so PHP code must use long <?php tags or the short-echo <?= tags; it MUST NOT use the other tag variations.

1.2 Character Encoding

PHP code MUST use only UTF-8 without BOM.

1.3 Side Efects

When you write your files, they should either declare stuff or cause side effects, but not both.

Side effects are things like output, changing settings, or including other files. Keep them separate from your code.

Good Example:

<?php
// Declare a function
function foo() {
    // Function stuff
}

// It's okay to check and declare
if (!function_exists('bar')) {
    function bar() {
        // More function stuff
    }
}
Enter fullscreen mode Exit fullscreen mode

Not So Good Example (mixing declarations and side effects):

<?php
// Change settings (a side effect)
ini_set('error_reporting', E_ALL);

// Include a file (another side effect)
include "file.php";

// Generate output (yet another side effect)
echo "<html>\n";

// Finally, declare a function
function foo() {
    // Function stuff
}
Enter fullscreen mode Exit fullscreen mode

2. Namespace and Class Names

In PHP, when it comes to namespaces and class naming, simplicity and consistency are key. Here's how it works:

  • Each class should reside in its own file.
  • Classes should be part of a namespace, at least one level deep, typically representing the vendor name.
  • Class names should be in StudlyCaps.

For instance:

// PHP 5.3 and later:
namespace Vendor\Model;

class Foo
{
}
Enter fullscreen mode Exit fullscreen mode

If you're working with PHP 5.2.x or an earlier version, it's a good practice to use a pseudo-namespacing approach, adding "Vendor_" prefixes to your class names:

// PHP 5.2.x and earlier:
class Vendor_Model_Foo
{
}
Enter fullscreen mode Exit fullscreen mode

This simple approach keeps your code organized and consistent across PHP versions.

3. Constants, Properties, and Methods

Last but not least, let's get into how you name stuff inside your classes.

3.1 Constants

Class constants should be in ALL CAPS with underscores.

Good Example:

<?php
namespace Vendor\Model;

class Foo {
    const VERSION = '1.0';
    const DATE_APPROVED = '2012-06-01';
}
Enter fullscreen mode Exit fullscreen mode

Not So Good Example (wrong casing and separator):

<?php
namespace Vendor\Model;

class Foo {
    const version = '1.0';
    const DateApproved = '2012-06-01';
}
Enter fullscreen mode Exit fullscreen mode

3.2 Properties

No strict rules here. You can use $StudlyCaps, $camelCase, or $under_score. Just be consistent in your codebase.

Good Example (using camelCase):

<?php
class SampleClass {
    public $sampleProperty;

    public function __construct() {
        $this->sampleProperty = "This is a sample property.";
    }
}
Enter fullscreen mode Exit fullscreen mode

Not So Good Example (inconsistent property naming):

<?php
class SampleClass {
    public $Sample_Property1;
    public $SampleProperty2;

    public function __construct() {
        $this->Sample_Property1 = "This is a sample property 1.";
        $this->SampleProperty2 = "This is a sample property 2.";
    }
}
Enter fullscreen mode Exit fullscreen mode

3.3 Methods

Use camelCase for method names.

Good Example:

<?php
class SampleClass {
    public function sampleMethod() {
        // Method stuff
    }
}
Enter fullscreen mode Exit fullscreen mode

Not So Good Example (using incorrect method naming):

<?php
class SampleClass {
    public function sample_method() {
        // Method stuff
    }
}
Enter fullscreen mode Exit fullscreen mode

Summary

  1. PHP Tags: Stick to <?php and <?= tags for your files.

  2. Character Encoding: Use UTF-8 without BOM.

  3. Side Effects: Your files should either declare symbols (like classes, functions, constants) or cause side effects (like generating output, altering .ini settings), but it's best not to mix the two.

  4. Namespaces and Classes: Each class gets its own file, and there's at least one level of a namespace, often representing the vendor name.

  5. Class Names: Class names should follow StudlyCaps.

  6. Class Constants: Class constants must be declared in all uppercase with underscores.

  7. Method Names: Method names must be declared in camelCase.

By following these simple rules, your PHP code stays clean and plays well with others.

Reference:

  1. PSR-1: Basic Coding Standard

Top comments (0)