Skip to content

Latest commit

 

History

History
62 lines (46 loc) · 2.87 KB

03-02-01-Programming-Paradigms.md

File metadata and controls

62 lines (46 loc) · 2.87 KB
isChild anchor
true
programming_paradigms

Programming Paradigms {#programming_paradigms_title}

PHP is a flexible, dynamic language that supports a variety of programming techniques. It has evolved dramatically over the years, notably adding a solid object-oriented model in PHP 5.0 (2004), anonymous functions and namespaces in PHP 5.3 (2009), and traits in PHP 5.4 (2012).

Object-oriented Programming

PHP has a very complete set of object-oriented programming features including support for classes, abstract classes, interfaces, inheritance, constructors, cloning, exceptions, and more.

Functional Programming

PHP supports first-class functions, meaning that a function can be assigned to a variable. Both user-defined and built-in functions can be referenced by a variable and invoked dynamically. Functions can be passed as arguments to other functions (a feature called Higher-order Functions) and functions can return other functions.

Recursion, a feature that allows a function to call itself, is supported by the language, but most PHP code is focused on iteration.

New anonymous functions (with support for closures) are present since PHP 5.3 (2009).

PHP 5.4 added the ability to bind closures to an object's scope and also improved support for callables such that they can be used interchangeably with anonymous functions in almost all cases.

Meta Programming

PHP supports various forms of meta-programming through mechanisms like the Reflection API and Magic Methods. There are many Magic Methods available like __get(), __set(), __clone(), __toString(), __invoke(), etc. that allow developers to hook into class behavior. Ruby developers often say that PHP is lacking method_missing, but it is available as __call() and __callStatic().