테스트 사이트 - 개발 중인 베타 버전입니다

[php 8.4] MyClass()->method() 를 이제 괄호없이 사용 가능해요

· 1년 전 · 856 · 3

 

[code]

// 괄호를 사용한 기존 방식 
$request = (new Request())->withMethod('GET')->withUri('/hello-world'); 

// PHP Parse error (<= PHP 8.3): syntax error, unexpected token "->" 
$request = new Request()->withMethod('GET')->withUri('/hello-world');
[/code]

 

8.4 부터 아래와 같은 표현이 가능합니다.

[code]

var_dump(

new MyClass()::CONSTANT, // string(8) "constant"

new MyClass()::$staticProperty, // string(14) "staticProperty"

new MyClass()::staticMethod(), // string(12) "staticMethod"

new MyClass()->property, // string(8) "property"

new MyClass()->method(), // string(6) "method"

new MyClass()(), // string(8) "__invoke"

);

[/code]

 

참고(영문): https://laravel-news.com/php-8-4-class-instantiation-without-extra-parenthesis 

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 3개

1년 전

감사 합니다.

1년 전

오! 많이 발전 됐군요 좋은 지식 감사합니다

감사 합니다.

게시글 목록

번호 제목
670
662
660
653
652
637
623
622
620
619
618
614
599
598
593
586
583
578
575
572
570
568
566
558
552
551
545
543
531
518