All posts by Pramod T P

HTTP Authentication

It is possible to use the header() function to send an “Authentication Required” message to the client browser causing it to pop up a Username/Password input window. Once the user has filled in a username and a password, the URL containing the PHP script will be called again with the predefined variables PHP_AUTH_USER, PHP_AUTH_PW, and AUTH_TYPE set to the user name, password and authentication type respectively. These predefined variables are found in the $_SERVER array. Both “Basic” and “Digest” (since PHP 5.1.0) authentication methods are supported Continue reading HTTP Authentication

Cross Site Scripting

Cross-Site Scripting (XSS) attacks are a type of injection, in which malicious scripts are injected into otherwise benign and trusted web sites. XSS attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser side script, to a different end user. Flaws that allow these attacks to succeed are quite widespread and occur anywhere a web application uses input from a user within the output it generates without validating or encoding it. Continue reading Cross Site Scripting

Trait

A Trait is simply a group of methods that you want include within another class. A Trait, like an abstract class, cannot be instantiated on it’s own.

As of PHP 5.4.0, PHP implements a method of code reuse called Traits.

A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own. It is an addition to traditional inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring inheritance.

  • Multiple traits can be used by a single class.
  • A trait can declare a private variable.
  • Traits are able to be auto-loaded.
<?php
 trait A {
 public function hello() {
 return "PHP";
 }
 public function world() {
 return "Codez";
 }
 }
 
 class test{
 use A ;
 }
 
 $obj = new test();
 echo $obj->hello()."\n";
 echo $obj->world()."\n";
 
?>

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.