Saturday, May 5, 2007

Syntax

PHP primarily acts as a filter. The PHP program takes input from a file or stream containing text and special PHP instructions and outputs another stream of data for display. From PHP 4, the PHP parser compiles input to produce bytecode for processing by the Zend Engine, giving improved performance over its interpreter predecessor. The Zend Engine II is at the heart of PHP 5.

The usual Hello World code example for PHP is:

echo 'Hello, World!';
?>

PHP only parses code within its delimiters, such as . Anything outside its delimiters is sent directly to the output and not parsed by PHP. The example above is equivalent to the following text (and indeed is converted into this form):

Hello, World!

The primary use of this is to allow PHP statements to be embedded within HTML documents, for example:

// PHP statements here
?>
Regular HTML here
// More PHP Statements
?>


Variables are prefixed with a dollar symbol and a type does not need to be specified in advance. Unlike function and class names, variable names are case sensitive. Both double-quoted ("") and heredoc strings allow the ability to embed the variable's value into the string.

PHP treats new lines as whitespace, in the manner of a free-form language (except when inside string quotes). Statements are terminated by a semicolon, except in a few special cases.

PHP has three types of comment syntax: /* */ which serves as block comments, and // as well as # which is used for inline comments.

Data types

PHP stores whole numbers in a platform-dependent range. This range is typically that of 32-bit signed integers. Integer variables can be assigned using decimal (positive and negative), octal and hexadecimal notations. Real numbers are also stored in a platform-specific range. They can be specified using floating point notation, or two forms of scientific notation.

PHP has a native Boolean type, named "boolean", similar to the native Boolean types in Java and C++. Using the Boolean type conversion rules, non-zero values can be interpreted as true and zero as false, as in Perl.

The null data type represents a variable that has no value. The only value in the null data type is NULL.

Variables of the "resource" type represent references to resources from external sources. These are typically created by functions from a particular extension, and can only be processed by functions from the same extension. Examples include file, image and database resources.

Arrays support both numeric and string indices, and are heterogeneous. Arrays can contain elements of any type that PHP can handle, including resources, objects, and even other arrays. Order is preserved in lists of values and in hashes with both keys and values, and the two can be intermingled.

Objects

Basic Object-oriented programming functionality was added in PHP 3. Handling of objects was completely rewritten for PHP 5, allowing for better performance and more features. In previous versions of PHP, objects were handled like primitive types. The drawback of this method was that the whole object was copied when a variable was assigned, or passed as a parameter to a method. In the new approach, objects are referenced by handle, and not by value. PHP 5 introduced private and protected member variables and methods, along with abstract classes and abstract methods. It also introduced a standard way of declaring constructors and destructors similar to that of other object-oriented languages, such as C++, and an exception handling model similar to that of other programming languages.

The static method and class variable features in Zend Engine 2 do not work the way some expect. There is no virtual table feature in the engine, so the static variables are bound with a name at compile time instead of with a reference.

If the developer asks to create a copy of an object by using the reserved word clone, the Zend engine will check if a __clone() method has been defined or not. If not, it will call a default __clone() which will copy all of the object's properties. If a __clone() method is defined, then it will be responsible to set the necessary properties in the created object. For convenience, the engine will supply a function that imports all of the properties from the source object, so that they can start with a by-value replica of the source object, and only override properties that need to be changed.

Classes in PHP 4 & 5 are defined using the class keyword, followed by the class name and its whether it is extending another class or not. After that the class members are contained in a set of curly braces. The following example demonstrates classes in PHP

Simple Class Definition

class ClassName //Defines class "ClassName"
{
function hello()
{
echo "Hello Word!";
}
}
?>

Extending Class Definition

You are able to extend a previously defined class using the extend keyword. If the parent class contains methods with the same name then they are replaced by the extender's (or child's) version. You can reference the parents version from within the extender class's using the parent:: scope reference.

class ClassNameExtended extends ClassName //Defines class "ClassNameExtended" which adds functionality to "ClassName"
{
function goodBye()
{
echo "GoodBye World!";
}
}
?>

Member function & variable types

PHP 5.0 added support for encapsulation within classes, and added the keywords public, protected and private for use in method signatures.

class ClassName //Defines class "ClassName"
{
public function hello() //[Public availability Creates a function available for use by any code (like standard function)
{
echo "Hello, World!";
}

private function goodBye() //[Private availability] This function is available only to the ClassName class
{
echo "GoodBye, World!";
}

protected function fooBar() //[Protected availability] This function is available only to the ClassName class and its extendors or parents
{
echo "Foo on you World!";
}

}
?>

The Constructor/Destructor Method

A class constructor is a method that gets called as soon as an object of that class is initialized. On the flip side, a destructor is a method that is called as the class is terminated. The constructor is generally used to set default property values or initial calculation results for a new object. The destructor is often used for cleaning up (eg. closing database connections). In PHP 4 the constructor is created by creating a method with the same name as the class. In PHP 5 the constructor is identified by using the __construct() method, and the destructor is identified using the __destruct() method. The example below shows a very basic but important use of a constructor. The __destruct method can also be called to terminate an instance of the class.


Using the __Construct/Destruct methods (PHP 5.0+ only)

class ClassName //Defines the class "ClassName"
{
public function __construct() //Define a function with the name "__construct"
{
echo "Class is Starting! ";
}

public function __destruct() //Define a function with the name "__destruct"
{
echo "Class is Ending! ";
}

public function hello()
{
echo "Hello function called! ";
}
}

$instance = new ClassName(); //Initiate an instance of the class. This calls the __construct function

$instance->hello(); // This will call the hello function and print the text

//When the script ends the class will automatically terminate and call the __destruct function.
?>

Using the older construct method

(PHP 4.0+ only)
Older versions (pre 5.0) of PHP also supported constructor methods that work in a similar method. The constructor is defined by creating a method with the same name as the parent class:

class ClassName //Defines the class "ClassName"
{
public function ClassName() //Define a function with the name "ClassName"
{
echo "Class is Starting! ";
}
}

$instance = new ClassName(); //Initiate an instance of the class. This is call the ClassName function
?>

No comments: