From left to right: Rasmus , Build 5.4 , Version 5.5 , Relise 5.6
function fn($reqParam, $optParam = null, ...$params) { }
function create_query($where, $order_by, $join_type = '', $execute = false, $report_errors = false) { }
create_query('deleted=0', 'name', default, default, /*report_errors*/ true);
use function foo\bar\baz;
baz();
<?php
function call_method($obj) {
$obj->method();
}
call_method(null); // oops!
try {
call_method(null); // oops!
} catch (EngineException $e) {
echo "Cool Exception: {$e->getMessage()}\n";
}
deprecated function myOldFunction() { }
new foo()->xyz;
new baz()->bar();
function fn($reqParam, $optParam = null, ...$params) {
var_dump($reqParam, $optParam, $params);
}
fn(1); // 1, null, []
fn(1, 2); // 1, 2, []
fn(1, 2, 3); // 1, 2, [3]
fn(1, 2, 3, 4); // 1, 2, [3, 4]
fn(1, 2, 3, 4, 5); // 1, 2, [3, 4, 5]
class MySQL implements DB {
protected $pdo;
public function query($query) {
$stmt = $this->pdo->prepare($query);
$stmt->execute(array_slice(func_get_args(), 1));
return $stmt;
}
// ...
}
$userData = $db->query('SELECT * FROM users WHERE id = ?', $userID)->fetch();
class MySQL implements DB {
public function query($query, ...$params) {
$stmt = $this->pdo->prepare($query);
$stmt->execute($params);
return $stmt;
}
// ...
}
$userData = $db->query('SELECT * FROM users WHERE id = ?', $userID)->fetch();
class Figure
{
public function calcPerimeter(array $angles)
{
return array_sum($angles);
}
}
class Square extends Figure
{
public function calcPerimeter($angle)
{
return 4 * $angle;
}
}
class Rectangle extends Figure
{
public function calcPerimeter($height, $width)
{
return 2 * ($height + $width);
}
}
$square = new Square();
var_dump($square->calcPerimeter(array(1, 1, 1, 1))); // 4
var_dump($square->calcPerimeter(1)); // 4
function create_query($where, $order_by, $join_type='', $execute = false, $report_errors = true) {...}
create_query("deleted=0", "name", default, default, /*report_errors*/ true);
namespace foo\bar {
function baz() {
return 'foo.bar.baz';
}
}
namespace foo\bar {
function qux() {
return baz();
}
}
namespace {
var_dump(foo\bar\qux());
}
namespace foo\bar {
function baz() {
return 'foo.bar.baz';
}
}
namespace {
use foo\bar as b;
var_dump(b\baz());
}
namespace foo\bar {
function baz() {
return 'foo.bar.baz';
}
function qux() {
return baz();
}
}
namespace {
use function foo\bar\baz, foo\bar\qux;
var_dump(baz());
var_dump(qux());
}
namespace foo\bar {
const baz = 42;
}
namespace {
use const foo\bar\baz;
var_dump(baz);
}
namespace {
use function foo\bar as foo_bar;
use const foo\BAZ as FOO_BAZ;
var_dump(foo_bar());
var_dump(FOO_BAZ);
}
namespace foo\bar {
function strlen($str) {
return 4;
}
}
namespace {
use function foo\bar\strlen;
use function foo\bar\non_existent;
var_dump(strlen('x'));
var_dump(non_existent());
}
namespace foo {
function bar() {}
class bar {}
}
namespace {
foo\bar(); // function call
new foo\bar(); // class instantiation
foo\bar::baz(); // static method call on class
}
namespace {
function bar() {}
}
namespace foo {
function bar() {}
}
namespace {
use foo\bar;
bar();
}
<?php
function call_method($obj) {
$obj->method();
}
call_method(null); // oops!
Fatal error: Call to a member function method() on a non-object in /path/file.php on line 4
Fatal error: Uncaught exception 'EngineException' with message 'Call to a member function method() on a non-object' in /path/file.php:4
Stack trace:
#0 /path/file.php(7): call_method(NULL)
#1 {main}
thrown in /path/file.php on line 4
try {
call_method(null); // oops!
} catch (EngineException $e) {
echo "Exception: {$e->getMessage()}\n";
}
// Exception: Call to a member function method() on a non-object
deprecated function myFunction() {
// ...
}
myFunction();
Deprecated: Function myFunction() is deprecated in ... on line 5
class MyClass {
public deprecated static function myMethod() {
// ...
}
}
MyClass::myMethod();
Deprecated: Function MyClass::myMethod() is deprecated in ... on line 7
class foo {
public $x = 'testing';
public function bar() {
return "foo";
}
public function baz() {
return new self;
}
static function xyz() {
}
}
var_dump(new foo()->bar()); // string(3) "foo"
var_dump(new foo()->baz()->x); // string(7) "testing"
var_dump(new foo()->baz()->baz()->bar()); // string(3) "foo"
var_dump(new foo()->xyz()); // NULL
new foo()->www(); // Fatal error: Call to undefined method foo::www()
class foo {
public $x = 1;
}
class bar {
public $y = 'foo';
}
$x = 'bar';
$bar = new bar;
var_dump((new bar)->y); // foo
var_dump((new $x)->y); // foo
var_dump((new $bar->y)->x); // 1
Source: https://habr.com/ru/post/198980/
All Articles