Login   Register  
PHP Classes
elePHPant
Icontem

File: php_5.2_example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Artur Graniszewski  >  Java and C# compatible scalar type hinting  >  php_5.2_example.php  >  Download  
File: php_5.2_example.php
Role: Example script
Content type: text/plain
Description: PHP 5.2 example (without namespaces)
Class: Java and C# compatible scalar type hinting
Check the types of called function arguments
Author: By
Last change:
Date: 2011-01-03 01:39
Size: 982 bytes
 

Contents

Class file image Download
<?php


ini_set
("display_errors"true);
error_reporting(E_ALL);
date_default_timezone_set('Europe/Warsaw');

require(
'./typehint.php');

$x = new TypeHint();
echo 
"<pre>";

function 
testString(string $a) {
    
var_dump($a);
}

function 
testBoolean(bool $a) {
    
var_dump($a);
}

function 
testInt(int $a) {
    
var_dump($a);
}

class 
test
{
    public static function 
staticTester(string $a) {
        
var_dump($a);
    }
    
    public function 
objectTester(bool $a) {
        
var_dump($a);
    }
}

$tester = new test();
$tester->objectTester(true);
test::staticTester("a");

// OK
testString("a");

// ERROR: Catchable fatal error: Argument 1 passed to testString() must be an instance of string, integer given, called in C:\lotos\public_html\example.php on line 30 and defined in C:\lotos\public_html\example.php on line 13
testString(12);

/*

// OK
testBool(true);

// ERROR
testBool(false);

*/

/*

// OK
testInt(12);

// ERROR
testInt("a");

*/