Skip to content

PHP sites

CS3305 - Web 2.0 - Week 3 - Discussion 3

  • What is the most popular server programming language?
  • Can you find which web applications or online systems are programmed with PHP?
  • What are some of the advanced features of PHP?

Solution

  • PHP is the most popular server programming language.
  • PHP is powering the backend for some of the most famous websites, including Moodle, Facebook, Wikipedia, WordPress, Yahoo, and Slack!
  • According to (Adams k, 2016), PHP originally stood for Personal Home Page in 1995.
  • Adams K. (2016). also mentioned some of PHP’s characteristics that made it suitable for big players:
    • State isolation, each request is isolated from the others and gets its own copy of the state. So if a request was faulty, it would not affect the other requests.
    • PHP opens a thread per request, so it can handle multiple requests simultaneously, and most importantly, each request execution is 100% isolated from the others.
    • No need to restart the server after a change in the code, which makes it easy to act quickly to bugs and security issues.

What are some of the advanced features of PHP?

  • PHP has many Advanced features in almost every aspect of the language; or third-party libraries that can be used to extend the language.
  • I will list a few of them, discussing each and pasting code examples or links to the documentation.

OOP

  • Object Oriented Programming was introduced in PHP 3; in 1998.
  • PHP now includes a complete object model that allows you to develop complex applications more efficiently.
  • Some of OOP features include:
    • Property and Method visibility modifiers: public, private, protected, final, and static.
    • Class constants: const keyword allows you to define constants inside a class, which can be accessed as a static property. using self::CONSTANT_NAME of ClassName::CONSTANT_NAME.
    • Class auto-loaders: functions that can be called to loaded to load classes automatically at the beginning of the script (request), reducing the number of import statements.
    • Destructor: a method that is called when an object is destroyed.
    • Inheritance: PHP supports single inheritance, meaning a class can only inherit from one parent class. Private and protected members of the parent class are not inherited.
    • Abstract classes: classes that cannot be instantiated but can be extended.
    • Interfaces: a way to specify what methods a class should implement.
  • More information can be found in the PHP documentation.

Namespaces

  • Namespaces are a way to group related classes, interfaces, functions, and constants.
  • Namespaces solve 2 problems:
    • Prevent name collisions between classes, interfaces, functions, and constants.
    • Shorter import statements by using aliases.
  • A namespace declaration looks like this: namespace MyProject; at the beginning of every PHP file that belongs to the namespace.
  • Then a namespace can be used like this: use MyProject\Sub\Level;.
  • A function can be called: nameSpace::functionName();.
  • More information can be found in the PHP documentation.

Traits

  • Traits are a way of code reuse. They allow you to reuse code in multiple classes.
  • Traits are similar to classes, but they cannot be instantiated.
  • Traits are used to group methods and properties that can be used in multiple classes.
  • When conflicting, methods from the current class will override the methods from traits.
  • Traits can be inherited by a class using the use keyword. e.g., use TraitName;.
  • Multiple Traits can be used in a class by separating them with a comma. e.g., use Trait1, Trait2;.
  • All objects instantiated from a class that uses a trait have those traits as methods they can use.
  • More on traits available on the PHP official documentation here: https://www.php.net/manual/en/language.oop5.traits.php
trait TraitName {
    public function sayHello() {
        echo 'Hello';
    }
}

class MyClass {
    use TraitName;
}

$obj = new MyClass();
$obj->sayHello(); // Hello

Closures

  • Closures are anonymous functions that can be used as variables, passed as arguments to other functions, and returned as values from other functions.
  • Anonymous functions are created using the Closure class.
  • Closures inherit variables from the parent scope in which they are created.
  • More information can be found in the PHP documentation.
$x = 1;
$printX = function() use ($x) {
    echo 'X is: ' . $x;
};

$printX(); // X is: 1

Generators

  • Generators simplify the creation of iterators.
  • Generators allow you to write code that behaves like a foreach loop without actually writing the loop or maintaining an array of values.
  • An example is range() function, which can generate a range of numbers.
foreach (range(1, 10) as $number) {
    echo $number;
}

Null Coalescing and Ternary Operators

  • The null coalescing operator ?? returns its first operand if it exists and is not null. Otherwise, it returns its second operand.
  • It is equivalent to the code below:
$var = isset($var) ? $var : 'default'; // Terenary operator
$x = $var ?? 'default'; // Null coalescing operator

Spaceship Operator

  • The spaceship operator <=> returns an integer less than, equal to, or greater than zero when the first operand is respectively less than, equal to, or greater than the second operand.
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

Constant Arrays using define()

  • Constant arrays can be defined using the define() function.
  • The define() function takes 3 parameters:
    • The name of the constant.
    • The value of the constant.
    • Whether the constant is case-sensitive, the default is false.
  • Constants are global by default
define('ANIMALS', array(
    'dog',
    'cat',
    'bird'
));

Group Use Declarations

  • Group use declarations allow you to import multiple classes/interfaces/functions/constants in a single statement.

List() and “…” Operator

  • List() is a language construct that allows you to assign a list of variables in one operation.
[$a, $b] = [1, 2];
echo $a; // 1
echo $b; // 2

Type Annotations

  • Type annotations are a way to specify the type of a variable or function arguments and return type.
  • Type annotations are static, so they are enforced at development time by the IDE and not at runtime by PHP.
  • During runtime, type annotations are ignored by default.
  • Opting into strict mode will cause PHP to throw a TypeError if a type annotation is violated.
  • More information can be found in the W3 Schools Tutorial

References