타입 힌팅

1 개요[ | ]

Type Hinting, function arguments type declarations
타입 힌팅, 함수 인수 자료형 선언
  • 함수 인수의 자료형을 명시하는 것
  • 잘못된 자료형이 들어오는 것을 감지할 수 있음
  • 런타임 중에 TypeError 예외 발생함

2 예시[ | ]

<?php
class C {}
class D extends C {}
class E {}

function f(C $c) {
    echo get_class($c)."\n";
}

f(new C);
f(new D);
# C
# D
f(new E);
# Fatal error: Uncaught TypeError: Argument 1 passed to f() must be an instance of C, instance of E given, called in - on line 14 and defined in -:6
# Stack trace:
# #0 -(14): f(Object(E))
# #1 {main}
#   thrown in - on line 8

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}