Home / Section index
 www.icosaedro.it 
PHPLint
Reference
Manual

Contents
Introduction
Basic features
Packages
Importing modules
Importing packages
Namespaces
Constants
Global variables
Types
Arrays
Control structures
Functions
Type compatibility
Typecasting
Predefined constants
Predefined superglobals
Classes
Recursive declarations
Errors
Exceptions
PHPLint's Std. Library
Autoloading classes
Definite assignment analysis
PHP4 - Classes
PHP4 - Type conversion operators
Documentator
Usage
How To...
Reserved keywords
To-do list
Memorandum
License
References
Syntax
<= PHP4 - ClassesDocumentator =>


PHP4 - Type conversion operators

A type conversion operator is a formal conversion of the type of a term. Neither the value of the term, nor its type are really changed. These type conversion operators are often required because some functions return a generic type mixed, array or object that does not fit the type of the expression where these function are called. Another typical case where a type conversion operator is required is to set the correct type for the NULL constant and the empty array array(): we already seen some examples in the chapter Functions.


Type conversion operator Applied to Description
/*. (string) .*/
/*. (array) .*/
/*. (array ...) .*/
/*. (resource) .*/
/*. (object) .*/
/*. (
CLASS_NAME ) .*/
null The NULL value can be formally converted into any type which is a reference in the PHPLint type model (see chapter Types).
/*. (bool) .*/
/*. (int) .*/
/*. (float) .*/
/*. (string) .*/
/*. (array) .*/
/*. (array ...) .*/
/*. (resource) .*/
/*. (object) .*/
/*. (
CLASS_NAME ) .*/
mixed A value of the type mixed can be formally converted to any type. The program should take care to check the type of the value before to do the conversion using one of these functions: is_bool(), is_int(), is_float(), is_string(), is_array(), is_resource(), is_object(). The class to which an object belong to can be checked with the function is_a() (PHP 4) or the binary operator instanceof (PHP 5).
Note that the exact structure of an array cannot be checked easily at run-time, so that the correcness of the conversion is in charge to the internal logic of the program.
Note that a value of the type mixed might be NULL; you can check this value either using the logical expression \$value===NULL or the function is_null(\$value); note that the original type to which this value belonged to cannot be guessed at run-time. Please, note the is_string(), is_array(), is_resource(), is_object() give FALSE if their argument is the value NULL.
/*. (array ... ) .*/ array This operator lets to declare the structure of a term of the generic type array. Note that this operator cannot be applied to an array of a known structure; an array of a given structure can be converted to an array of a different structure using a specific algorithm.
/*. (object) .*/
/*. (
CLASS_NAME ) .*/
object This operator converts the type of a generic object value to the instance of the given class name. Before to apply this operator, the program should check the actual class to which the object belong to using the instanceof binary operator (PHP 5) or the is_a() function (PHP 4).

Examples:
$abc = array("a", "b", "c");
$cba = /*. (array[int]string) .*/ array_reverse($abc);
/* Reverse the order of the elements of the array $abc.
   Note that the formal type-cast conversion operator is
   required because the function array_reverse() returns
   a generic array, so loosing info about the actual
   structure of the resulting array. */

class A { }
$obj = /*. (A) .*/ NULL;
/* This variable is an object initially NULL. Since NULL
   is not a well defined type, a formal type-cast is required. */

$arr = /*. (array[int]string) .*/ array();
/* An array of strings with int index, initially empty. Since
   the empty array does not allows to PHPLint to guess neither the
   type of the index nor the type of the elements, a formal
   type-cast is required. */


class A_DOG { function bark(){} }
class A_CAT { function miaow(){} }

function Play(/*. object .*/ $obj)
/*
    Since this function accepts an object of different
    classes, type conversion operators will be required.
*/
{
    if( $obj instanceof A_DOG ){
        echo "It's a dog";
        $dog = /*. (A_DOG) .*/ $obj;
        $dog->bark();
        /* NOTE: $obj->bark() would be correct for PHP, but
           it would not for PHPLint, since a generic object
           does not has the method bark().  That's why a new
           variable $dog needs to be introduced. */
    } else if( $obj instanceof A_CAT ){
        echo "It's a cat";
        $cat = /*. (A_CAT) .*/ $obj;
        $cat->miaow();
    } else {
        echo "It's an unexpected animal";
    }
}

Play(new A_DOG());
Play(new A_CAT());

The last example involving cats and dogs is a typical example of how programs should not be written. PHP 5 introduces the interfaces to resolve in a more elegant way problems like this. PHP 4 does not has interfaces, but still PHPLint allows to declare abstract classes with which a similar result can be achieved.



<= PHP4 - ClassesDocumentator =>

Umberto Salsi

Contact
Site map
Home / Section index