Reflection

Reflection is generally defined as a program’s ability to inspect itself and modify its logic at execution time. In less technical terms, reflection is asking an object to tell you about its properties and methods, and altering those members (even private ones).

Reflection is where an object is able to introspectively examine itself to inform you of it’s methods and properties at runtime.

On the face of it this doesn’t seem like something that would be particularly useful. However, Reflection is actually a really interesting aspect of software development and it is something that you will probably touch upon often.

Following is possible using reflection

  • Analysing of nearly any aspect of classes and interfaces
  • Analysing of nearly any aspect of functions
  • Implement dynamic construction (new with variable class name)
  • useful is for debugging your code. You’ve probably used the get_class() and get_class_methods() functions when working with an ambiguously named object.
  • The ability to get the type or methods of an object when you don’t know what type of object it is, is Reflection.
  • Another common use of Reflection is for creating documentation.
  • Reflection can instantiate objects.
  • Reflection can modify static proprties of the class.
  • Reflection can get the namespace name of a class.
  • Reflection can not modify static variable in functions.

Example

<?php
 class A {
 public $one = '';
 public $two = '';
 
 public function echoOne() {
 echo $this->one."\n";
 }
 
 public function echoTwo() {
 echo $this->two."\n";
 }
 }

$a = new A();
 $reflector = new ReflectionClass('A');
 $properties = $reflector->getProperties();
 foreach($properties as $property) {
 echo "<pre>";print_r($property);
 }
?>

bindTo

Closure::bindTo — Duplicates the closure with a new bound object and class scope

Create and return a new anonymous function with the same body and bound variables as this one, but possibly with a different bound object and a new class scope.

The “bound object” determines the value $this will have in the function body and the “class scope” represents a class which determines which private and protected members the anonymous function will be able to access. Namely, the members that will be visible are the same as if the anonymous function were a method of the class given as value of the newscope parameter.

Static closures cannot have any bound object (the value of the parameter newthis should be NULL), but this function can nevertheless be used to change their class scope.

This function will ensure that for a non-static closure, having a bound instance will imply being scoped and vice-versa. To this end, non-static closures that are given a scope but a NULL instance are made static and non-static non-scoped closures that are given a non-null instance are scoped to an unspecified class.

Closure

A Closure is essentially the same as a Lambda apart from it can access variables outside the scope that it was created.

For example:
<?php
 $user = "PHPCodez";
 // Create a Closure 
 $greeting = function() use ($user) { 
 echo "Hello $user"; 
 };

$greeting();

As you can see above, the Closure is able to access the $user variable. because it was declared in the use clause of the Closure function definition.

If you were to alter the $user variable within the Closure, it would not effect the original variable. To update the original variable, we can append an ampersand. An ampersand before a variable means this is a reference and so the original variable is also updated.

Closures are also useful when using PHP functions that accept a callback function like array_map, array_filter, array_reduce or array_walk.

The array_walk function takes an array and runs it through the callback function.

Again, you can access variables outside the scope of the Closure by using the use clause:

In the example above, it probably wouldn’t make sense to create a function to just multiply two numbers together. If you were to create function to do a job like this, then come back to the code a while later you will probably be thinking why did you bother create a globally accessible function only to be used once? By using a Closure as the callback, we can use the function once and then forget about it.

Anonymous Functions

An anonymous function is simply a function with no name.

Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses.

Anonymous functions are implemented using the Closure class.

  • Anonymous functions can be bound to objects
  • Methods bind() and bindTo() of the Closure object provide means to create closures with different binding and scope
  • Binding defines the value of $this and the scope for a closure

An anonymous function has no name so you would define it like this

// Anonymous function
 function () {
 return "PHP Codez";
 }

Anonymous function variable assignment

<?php
$greet = function($name)
{
 printf("Hello %s\r\n", $name);
};

$greet('Codez');
$greet('PHP');
?>

Lambda

A Lambda is an anonymous function that can be assigned to a variable or passed to another function as an argument. If you are familiar with other programming languages like Javascript or Ruby, you will be very familiar with anonymous functions.

Because the function has no name, you can’t call it like a regular function. Instead you must either assign it to a variable or pass it to another function as an argument.

Lambdas are useful because they are throw away functions that you can use once. Often, you will need a function to do a job, but it doesn’t make sense to have it within the global scope or to even make it available as part of your code. Instead of having a function used once and then left lying around, you can use a Lambda instead.

Of course, you have been able to use the create_function function in PHP for a while now. This basically does the same job.