Clone. Die/Exit. Global. XOR. Callable.

CLONE

By default, the clone command will create a shallow copy of an object. This means that all primitive properties will be copied into a new object of the same type, but references will continue to reference the same object.

This behaviour can be modified in your own classes by implementing the __clone() magic method, which will be run on the cloned object immediately after that object is cloned.

<?php
$date = new DateTime();

// Clone the date to create a new date object which can be manipulated
// without affecting the initial date object
$newDate = clone $date;

DIE / EXIT

These two items are equivalent. When you use die or exit, the script immediately exits.

It can take an optional parameter of either a string, which will be displayed to the user, or an integer exit code between 0 and 254, which is only relevant if you’re writing a command line script.


GLOBAL

The global keyword allows you to get and set a variable from the global scope. This means that a variable can be defined and referenced from anywhere within your codebase.

Using global is heavily contraindicated. Globals tightly couple various unrelated sections of your code together, and make it so that it’s difficult to reason about where a variable has been defined, or when it was updated.

Note: If you must use a global variable, consider using the $GLOBALS array. This is a superglobal, available from anywhere, and it makes it clear that you’re using or updating a global variable.

<?php
$counter = 0;

function incrementCounter()
{
    global $counter;
    
    $counter += 1;
    echo $counter;
}

incrementCounter();

XOR

The xor (exclusive or) operator is a logical operator that will result in true if only one of its operands are true. If neither is true, or both of them are true, the result of the expression is false.

<?php
// False
$a = false xor false;
$b = true xor true;

// True
$c = false xor true;
$d = true xor false;

CALLABLE

This keyword can only be used as a type hint or return type, and indicates that it only accepts or returns a value of the following form:

  • A closure / anonymous function
  • A string with the name of an existing function
  • An array containing an object as its first value and the name of a method as its second value
  • An array containing the name of a class as its first value and the name of a static method as its second value

A callable can be run by adding arguments inside brackets to the variable name, e.g. $callable($argument).

<?php
function runCallable(callable  $callable): callable 
{
    // Runs the passed in function
    $callable("I am a callable");
    
    return $callable;
}

class SimpleClass
{
    public static function runStatic(string $message)
    {
        echo $message;
    }
    
    public function run(string $message)
    {
        echo $message;
    }
}

function simpleFunction(string $message)
{
    echo $message;
}

$closure = function(string $message)
{
    echo $message;
};

$function = "simpleFunction";
$method = [new SimpleClass(), "run"];
$staticMethod = [SimpleClass::class, "runStatic"];

runCallable($closure);
runCallable($function);
runCallable($method);
runCallable($staticMethod);