preg_match_all

The preg_match_all() function matches all occurrences of pattern in string.

It will place these matches in the array pattern_array in the order you specify using the optional input parameter order. There are two possible types of order −

PREG_PATTERN_ORDER − is the default if the optional order parameter is not included. PREG_PATTERN_ORDER specifies the order in the way that you might think most logical; $pattern_array[0] is an array of all complete pattern matches, $pattern_array[1] is an array of all strings matching the first parenthesized regexp, and so on.

PREG_SET_ORDER − will order the array a bit differently than the default setting. $pattern_array[0] will contain elements matched by the first parenthesized regexp, $pattern_array[1] will contain elements matched by the second parenthesized regexp, and so on.

The function returns  the number of matching.

<?php
 $text = <<<EOT
 The big bang bonged under the bung.
 EOT;
 echo preg_match_all('@b.n?g@', $text, $matches);
 print_r($matches);
?>

Final Class

Final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

  • A final class may be instantiated.
  • A class with a final function may be derived.
  • Static functions can be final.
  • Preventing massive inheritance chain of doom
  • Encouraging composition
  • Force the developer to think about user public API
  • Force the developer to shrink an object’s public API
  • A final class can always be made extensible
  • extends breaks encapsulation
  • You don’t need that flexibility
  • You are free to change the code

Stream Contexts

Streams are resources provided by PHP that we often use transparently, but which can also be very powerful tools. By learning how to harness their power, we can take our applications to a higher level.

Every stream has a implementation wrapper which has the additional code necessary to handle the specific protocol or encoding. PHP provides some built-in wrappers and we can easily create and register custom ones. We can even modify or enhance the behavior of wrappers using contexts and filters.

A context can modify or enhance the behavior of a stream

A context is a set of parameters and stream wrapper specific options

The following functions accepts a stream $context parameter.

  • fopen
  • file_get_contents
  • file

SplObjectStorage

The SplObjectStorage class provides a map from objects to data or, by ignoring data, an object set. This dual purpose can be useful in many cases involving the need to uniquely identify objects.

  • It uses Objects are indexes
  • It can be used to implement set of objects
  • It allows arbitrary data to be associated with objects
  • It does not permit the serialization of objects

Type Hinting

With Type hinting we can specify the expected data type (arrays, objects, interface, etc.) for an argument in a function declaration. This practice can be most advantageous because it results in better code organization and improved error messages.

You can use type hinting to specify the expected data type of an argument in a function declaration. When you call the function, PHP will check whether or not the arguments are of the specified type. If not, the run-time will raise an error and execution will be halted.

  • Typehints can be optional
  • Type hints can be reference
  • Typehints parameters can default to NULL.
  • Type hints class does not have to be defined when a function definition is parsed
  • Objects need not be of the same class to satisfy typehinting.
  • Type hints can be PHP scalar value

SplFixedArray

The SplFixedArray class provides the main functionalities of array. The main differences between a SplFixedArray and a normal PHP array is that the SplFixedArray is of fixed length and allows only integers within the range as indexes. The advantage is that it allows a faster array implementation.

property_exists

This function checks if the given property exists in the specified class (and if it is accessible from the current scope).

It returns TRUE if the property exists, FALSE if it doesn’t exist or NULL in case of an error.

<?php
 $a = array('a'=>'a', 'b'=>'c');
 echo property_exists((object) $a, 'a')?'true':'false';
 ?>

true-true will be the output

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