Skip to content
Jorge Estanislao Barsoba edited this page Jul 17, 2024 · 9 revisions

Must-Knows - PHP

Intro

  • Created by Rasmus Lerdorf in 1994
  • PHP is a recursive acronym that means "PHP Hypertext Preprocessor"
  • Interpreted

Type system

  • Dynamic (weak-typed), as type-checking is performed at runtime
  • Nominal, as the name of the class determines the type of an object, like in C#; So, it's not Duck-typed
  • Type juggling: PHP does not support explicit type definition in variable declaration; i.e. If a string value is assigned to variable $var, $var becomes a string
  • Type coercion: Automatic conversion of values from one data type to another, such as strings to numbers; e.g. is_numeric("5.6") will return true (like JavaScript)
  • Type declarations: Can be added to function arguments, return values and, since v7.4, class properties

Variables

Types

  1. String
  2. Integer
  3. Float
  4. Boolean
  5. Null
  6. Array
  7. Object
  8. Resource

Declaration

$name = 'Jorge';
$age = 41;
$isMale = true;
$height = 1.81;
$salary = null;

Constants

Regular constants

define('PI', 3.14);
echo PI;
var_dump(defined('PI')); // true
echo PHP_INT_MAX; // 9223372036854775807

Predefined constants: https://www.php.net/manual/en/reserved.constants.php.

Magic constants

Constants that change their value depending on where they're called from, e.g. __DIR__, __FILE.

Associative arrays

PHP-specific type of arrays that use named keys that you assign to them:

$age = array("Jorge" => 41, "Pau" => 42, "Male" => 4);

JSON / Associative array utility functions:

// Convert JSON into Associative array
$json = file_get_contents('todo.json');
$jsonArray = json_decode($json, true);
$jsonArray[$todoName] = ['completed' => false];

// Convert Associative array back into JSON
file_put_contents('todo.json', json_encode($jsonArray, JSON_PRETTY_PRINT));

Equality operators

Same as JavaScript:

  • == just checks the values, not the types
  • === values and types

Superglobals

Predefined variables available in all scopes, such as $_SERVER.

Doc-blocks

  • /** ... */ is a special syntax in PHP called a doc-block
  • Discoverable at runtime and used by different frameworks, e.g. Behat:
      /**
      * @Given there is a(n) :arg1, which costs £:arg2
      */
      public function thereIsAWhichCostsPs($arg1, $arg2)
      {
          throw new PendingException();
      }

Final keyword

When used on class declaration, it prevents that class from being extended. Use decorators instead of inheritance:

<?php
final class Foo
{
    public method doFoo()
    {
        // do something useful and return a result
    }
}

final class FooDecorator
{
    private $foo;
   
    public function __construct(Foo $foo)
    {
        $this->foo = $foo;
    }
   
    public function doFoo()
    {
          $result = $this->foo->doFoo();
          // ... customize result ...
          return $result;
    }
}
?>

Countable

In PHP, classes can implement the Countable interface and its count method:

final class Basket implements Countable {
    private array $products;
    
    ...

    public function count(): int {
        return count($this->products);
    }
}

Then Basket will be countable:

PHPUnit_Framework_Assert::assertCount(intval($count), $this->basket);

VSCode / PhpStorm Ubuntu setup

PhpStorm

  • Use VSCode keymap
  • Override Ctrl+Shift+G (Git) to open Commit sidebar instead
  • Disable conflicting shortcuts from Ubuntu

Global namespace

A \ before a class name or a function will ensure this is called from the global namespace, even if there's sth with the same name in the current namespace.

Clone this wiki locally