isChild | anchor |
---|---|
true |
programming_paradigms |
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).
PHP has a very complete set of object-oriented programming features including support for classes, abstract classes, interfaces, inheritance, constructors, cloning, exceptions, and more.
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.
- Continue reading on Functional Programming in PHP
- Read about Anonymous Functions
- Read about the Closure class
- More details in the Closures RFC
- Read about Callables
- Read about dynamically invoking functions with
call_user_func_array()
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()
.