PHP 5.6 рдореЗрдВ рдХрд╛рд░реНрдп - рдирдпрд╛ рдХреНрдпрд╛ рд╣реИ?


рдмрд╛рдПрдВ рд╕реЗ рджрд╛рдПрдВ: рд░рд╛рд╕рдорд╕ , рдмрд┐рд▓реНрдб 5.4 , рд╕рдВрд╕реНрдХрд░рдг 5.5 , 5.6 5.6

рдЖрдЬ рдореИрдВ рдЕрдкрдиреА рджреГрд╖реНрдЯрд┐ рдХреЛ рд╕рд╛рдЭрд╛ рдХрд░рдирд╛ рдЪрд╛рд╣рддрд╛ рд╣реВрдВ рдХрд┐ рдЕрдЧрд▓реЗ рдкреНрд░рдореБрдЦ PHP рд░рд┐рд▓реАрдЬ рдореЗрдВ рдХрд╛рд░реНрдпреЛрдВ рдХреЗ рд╕рд╛рде рдХрд╛рдо рдХреИрд╕реЗ рджрд┐рдЦреЗрдЧрд╛ - 5.6ред рдРрд╕рд╛ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП, рдореИрдВрдиреЗ рдХрд╛рдо рдХреЗ рдкреНрд░рд╕реНрддрд╛рд╡реЛрдВ рдХрд╛ рдЕрдзреНрдпрдпрди рдХрд┐рдпрд╛ рдФрд░ рд╡рд╣рд╛рдВ рдмрд╣реБрдд рд╕рд╛рд░реА рдорд┐рдард╛рдИ рдкрд╛рдИ:

. .

: , RFC. , тАФ internals@lists.php.net.

RFC, 5.6.

PHP 5.6, 36 1

: , ...$params :
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]

$params , , . $params ( ). $params 0 .

func_get_args(). , MySQL:
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();

.
-, public function query($query) , , , . , .

-, func_get_args() , , $query array_slice(func_get_args(), 1).

RFC :
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();

...$params , , $query $params. , .


  • function fn($arg, ...$args): $args
  • function fn($arg, &...$args):
  • function fn($arg, array ...$args): , ( )


  • : , , .
  • array_slice() func_get_args()



тАФ !

internals@lists.php.net

, : ( / ) :
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

. , - func_get_args(). , , , if-else ( ). - , C++, Java/Scala, Delphi . , тАФ . .



PHP , :
function create_query($where, $order_by, $join_type='', $execute = false, $report_errors = true) {...}

, . , $report_errors, ? . , , .

, , , , :
create_query("deleted=0", "name", default, default, /*report_errors*/ true);

, $join_type $execute . , . , , .


, ZEND_NUM_ARGS(). , PECL, using zend_parse_parameters zval-type , . , , default . , .

16 4, PHP 5.6

PHP (, , ) use. , . , .

, , :
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());
}

. PHP .

, , . .

use , , , .

, use function:
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);
}



?

, . , , . , . , . , , : igorw\compose(). . , , compose(). , .


PHP , . , use, .
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());
}

strlen() . non_existent() .

use function, use?

PHP . foo\bar foo\bar , , ( ):
namespace foo {
    function bar() {}
    class bar {}
}
 
namespace {
    foo\bar(); // function call
    new foo\bar(); // class instantiation
    foo\bar::baz(); // static method call on class
}

use , .

:
namespace {
    function bar() {}
}
 
namespace foo {
    function bar() {}
}
 
namespace {
    use foo\bar;
    bar();
}

, use. PHP, .


, PHP 5.6

RFC .

, :
<?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

RFC EngineException. , :
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

E_RECOVERABLE_ERROR

, . , , , .

( ). , ErrorException, , .

Catch-all

EngineException Exception, catch Exception. . BaseException, Exception. BaseException , . Python (BaseException) Java (Throwable).


deprecated



RFC deprecated , ZEND_ACC_DEPRECATED, , E_DEPRECATED.

?

тАФ PHP . . ZEND_ACC_DEPRECATED , Zend E_DEPRECATED. , @deprecated ( ) E_USER_DEPRECATED. , ?

deprecated, , .

E_USER_DEPRECATED .

, ReflectionFunction::isDeprecated() . RFC .

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




RFC тАФ . .

1 ( )


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()

2 ( )


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



, . , ! , :)

, , RFC. , , - :)

!


Source: https://habr.com/ru/post/In198980/


All Articles