<?php
/*
* User.php
*/
namespace MySpace;
class User {
function say() {
echo 'Hello World';
}
}
/*
* Application.php
*/
namespace Project;
require 'User.php';
use MySpace\User;
$user = new User();
$user->say();
<?php
// This scrips will only output "Bar"
goto a;
echo 'Foo';
a:
echo 'Bar';
<?php
// Simplest use: using a Closure as a "parameter" (second)
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
<?php
const CONSTANT = 'THE SECRET';
This new feature is pretty big to just drop a code sample here, but make sure you check it out here.
<?php
// Normally a Ternary looks like:
$return = (expr1) ? (expr2) : (expr3)
// Using the new shorthand for is:
$return = (expr1) ?: (expr3)
// Returns expr1 if expr1 evaluates to TRUE, or expr3 otherwise
<?php
class C {
public static $foo = 123;
}
$a = "C";
echo $a::$foo;
<?php
class MyCustomException extends Exception {}
try {
throw new MyCustomException("Exceptional", 112);
} catch (Exception $e) {
/* Note the use of the third parameter to pass $e
* into the RuntimeException. */
throw new RuntimeException("Rethrowing", 911, $e);
}
For a full list of changes go here.