php-annotated-monthly-december-2018 - 2. PHP Internals

Php_annotated_monthly 이미지

JetBrains에서 (거의) 매월 발행하는 PHP Annotated Monthly에서 적당히 제 입맛에 맞는 것만 추려 소개합니다. 언제나 그렇듯이 성급한 오역 죄송합니다.

이번달은 내용이 많아 쪼개서 올리고 있습니다. 두번째는 PHP Internals!


[RFC] Preloading

https://wiki.php.net/rfc/preload

— The proposal is now accepted! It means that in 7.4, we’ll be able to preload any files into opcache. Any functions or classes declared in preloaded files will be available to all subsequent requests as if they were internal elements like strlen() or Exception.

There is an ongoing discussion about supporting preloading in Composer. Dmitry Stogov also proposed an ability to disable opcache caching per script, using declare(cache=0) at the start of PHP files.

다음 버전인 PHP7.4부터 preloading을 지원합니다. opcache.preload에 PHP 파일을 지정하고, 여기서 preloading할 파일을 지정할 수 있습니다. 이렇게 컴파일된 코드는 공유 메모리에 상주하게 되고, preload된 파일이 후에 변경된다 하더라도 서버 재시작 되기 전까지는 동일한 결과를 돌려주게 됩니다.

Composer를 지원할지에 대해선 여전히 논의중이라고 하는군요.

위 RFC 링크에 들어가면 더 자세한 설명이 들어있습니다. 여기에선 Zend 라이브러리를 통째로 올리는 예제가 있는데요. Laraval처럼 무거운 프레임웍도 미리 올려두고 쓰면 성능이 비약적으로 좋아지지 않을까 예상됩니다.

몇가지 주의할 점도 언급하고 있는데요.

And also, this approach will not be compatible with servers that host multiple applications, or multiple versions of applications - that would have different implementations for certain classes with the same name - if such classes are preloaded from the codebase of one app, it will conflict with loading the different class implementation from the other app(s).

동일한 서버에서 여러 어플리케이션을 사용할 때, 동일한 클래스명을 사용하면 preload된 클래스를 사용하게 되므로 문제가 발생할 수 있다는 것입니다.

[RFC] Spread Operator in Array

https://wiki.php.net/rfc/spread_operator_for_array

— The document proposes supporting the … operator in arrays. For example,

$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];

Spread Operator도 나올 때가 됐지요! 기대됩니다.

[RFC] FFI – Foreign Function Interface

https://wiki.php.net/rfc/ffi

— Dmitry Stogov’s experiment is now official RFC. It allows calling C functions and using C data types from pure PHP. See for example using TensorFlow with FFI.

Dmitry란 이름을 가진 사람은 다 개발을 잘하는 것 같습니다(농담입니다…).

PHP 상에서 C의 함수와 데이터 타입에 접근할 수 있는 기능입니다. 링크를 타고 들어가면 여러가지 예제를 볼 수 있습니다. 그중 shared library에서 함수를 가져와 실행하는 예제를 보시죠.

<?php
// create FFI object, loading libc and exporting function printf()
$ffi = FFI::cdef(
"int printf(const char *format, ...);", // this is regular C declaration
"libc.so.6");
// call C printf()
$ffi->printf("Hello %s!\n", "world");

FFI 역시 위에 언급한 preloading과 잘 연계되도록 설계가 되었고, default로 preload 모드로 동작합니다.

과연 이걸 직접 사용할 날이 오겠냐마는, 이를 활용한 라이브러리가 여럿 등장하지 않을까 기대됩니다. 어디에 써먹냐고요? 그건 제가 알 바가…

[RFC] Covariant Returns and Contravariant Parameters

https://wiki.php.net/rfc/covariant-returns-and-contravariant-parameters

공변 리턴과 반변 파라미터도 소개가 됐습니다.

interface X {
function m(Y $z): X;
}
interface Y extends X {
// not permitted but type-safe
function m(X $z): Y;
}

Super type인 X의 메소드에서 파라미터로 Y를 받게 되어있다면 sub type인 Y의 메소드에서도 Y를 받게 되어 있습니다. Y가 X의 sub type이라 해도 Y대신 더 추상적 개념인 X를 받을 수 없는 상황이죠. 반대로 리턴 타입의 경우 X를 리턴하기로 했으면 더 구체적인 클래스인 Y를 리턴할 수 없었습니다.

이 RFC는 parameter type은 super type이 대리할 수 있고, return type은 sub type이 대리할 수 있게 허용하는 것입니다.