Pages

11 Apr 2014

PHP Advanced Topics with examples

Superglobals
In the previous chapters the concept of global variable was explained. A global variable is a variable declared at the top of the script outside the function. This variable is available to the complete script. Superglobal variables are arrays built into PHP. These superglobal variables are populated automatically with useful elements, and they are available in any scope. A superglobal array can be accessed within a function or a method without using the global keyword.
PHP Superglobals
  • $_COOKIE – It contains values provided to the script via HTTP cookies.

  • $_GET – It contains variables submitted to the script using HTTP get method.

  • $_POST – It contains variables submitted to the script using HTTP post method.

  • $_REQUEST – It is a combined array containing values from the $_GET, $_POST, and $_COOKIES superglobal arrays.

  • $_ENV – It contains keys and values set by the script’s shell.

  • $_FILES – It contains information about uploaded files.

  • $_SERVER – It contains variables made available by the server.

  • $GLOBALS – It contains all the global variables associated with the current script.
Instanceof Operator
The instanceof operator in PHP is used to determine whether a given object, the parents of that object, or its implemented interfaces are of a specified object class.
Example
<?php
class X { }
class Y { }
$thing = new X;
if ($thing instanceof X) {
echo 'X';
}
if ($thing instanceof Y) {
echo 'Y';
}
?>
 
Declare
The declare statement is used to set execution directives for a block of code. The syntax of the declare statement is similar to the syntax of other flow control statements.
Syntax
declare (directive)
statement
 
The directive section allows the behavior of the declare block to be set. In this only one directive is recognized, which is the ticks directive.
[ Ticks - A tick is an event that occurs for every N low level statements, which are executed within the declare block. The value of N is specified using the statement ticks = N in the directive section.]
The statement part of the declare block will be executed according to the directives set in the directive block.
Example
<?php
// how to use declare:
// the first way:
declare(ticks=1) {
// entire script here
}
// the second way:
declare(ticks=1);
// entire script here
?>

Php Advanced Class Concepts with example

Constructor
In PHP 5 developers can declare constructor methods for classes. In this, those classes which have a constructor method call this method for each newly created object. So, it is suitable for any initialization that the object may need before it is used. In this the parent constructors are not called implicitly if the child class defines a constructor.
Example
<?php

class ParentClass {
function __construct() {
print "In ParentClass constructor\n";
}
}
class ChildClass extends ParentClass {
function __construct() {
parent::__construct();
print "In ChildClass constructor\n";
}
}
$obj = new ParentClass();
$obj = new ChildClass();
?>
 

Destructors
The destructor concept is introduced in PHP 5. This destructor concept is similar to other object oriented languages. In this the destructor will be called as soon as all references to a particular object have been removed or when the object has been explicitly destroyed.
Example
<?php
class MyClass {
function __construct() {
print "In constructor\n";
$this->name = "MyClass";
}
function __destruct() {
print "Destroying " . $this->name . "\n";
}
}
$obj = new MyClass();
?>
 
Patterns
Patterns are ways to describe best practices and good designs. The patterns show a flexible solution to common programming problems.
Factory Pattern
In the factory pattern the objects are instantiated at runtime. It is called a factory pattern since it is responsible for manufacturing an object. A parameterized factory receives the name of the class to instantiate as argument.
Example
<?php
class Welcome
{
// The parameterized factory method
public static function factory($type)
{
if (include_once 'Drivers/' . $type . '.php') {
$classname = 'Driver_' . $type;
return new $classname;
} else {
throw new Exception ('Driver not found');
}
}
}
?>