Login   Register  
PHP Classes
elePHPant
Icontem

File: php_5.3_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.3_example.php  >  Download  
File: php_5.3_example.php
Role: Example script
Content type: text/plain
Description: PHP 5.3 example (with 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:40
Size: 999 bytes
 

Contents

Class file image Download
<?php

namespace Lotos;
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");

*/