svn: /phpdoc/kr/trunk/language/oop5/ typehinting.xml

From: Date: Sun, 28 Dec 2014 13:03:52 +0000
Subject: svn: /phpdoc/kr/trunk/language/oop5/ typehinting.xml
Groups: php.doc.kr 
Request: Send a blank email to doc-kr+get-1977@lists.php.net to get a copy of this message
acidd15 Sun, 28 Dec 2014 13:03:52 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=335422 Log: initial translate Changed paths: A phpdoc/kr/trunk/language/oop5/typehinting.xml Added: phpdoc/kr/trunk/language/oop5/typehinting.xml =================================================================== --- phpdoc/kr/trunk/language/oop5/typehinting.xml (rev 0) +++ phpdoc/kr/trunk/language/oop5/typehinting.xml 2014-12-28 13:03:52 UTC (rev 335422) @@ -0,0 +1,175 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- $Revision$ --> +<!-- EN-Revision: 331202 Maintainer: acidd15 Status: ready --> +<!-- Reviewed: no --> + + <sect1 xml:id="language.oop5.typehinting" xmlns="http://docbook.org/ns/docbook"> + <title>타입 힌트</title> + <para> + PHP 5 에서 타입 힌트를 도입했습니다. 함수는 이제 파라미터를 객체(함수 프로토 타입에 클래스의 이름을 명시하는것으로), + 인터페이스, 배열(PHP 5.1 이후) 또는 <type>callable</type>(PHP 5.4 이후) 로 강제할수 있게 되었습니다. + 하지만, <type>NULL</type> 을 디폴트 값으로 지정하게 되면, 이후 호출시 NULL을 인자로 넘기는것도 가능합니다. + </para> + + <para> + 클래스나 인터페이스가 타입힌트로 사용되면 그의 모든 자식이나 구현체들은 인자로 넘기는게 가능합니다. + </para> + + <para> + 타입 힌트는 <type>int</type> 또는 <type>string</type> 과 같은 스칼라 타입은 사용될수 없습니다. + <link linkend="language.types.resource">리소스</link> 와 <link linkend="language.oop5.traits">트레이트</link> 또한 사용될 수 없습니다. + </para> + + <example> + <title>타입 힌트 예제</title> + <programlisting role="php"> +<![CDATA[ +<?php +// An example class +class MyClass +{ + /** + * A test function + * + * 첫번째 파라미터는 OtherClass 타입의 객체여야 함. + */ + public function test(OtherClass $otherclass) { + echo $otherclass->var; + } + + + /** + * Another test function + * + * 첫번재 파라미터는 배열 이어야 함. + */ + public function test_array(array $input_array) { + print_r($input_array); + } + + /** + * 첫번째 파라미터는 iterator 이어야 함. + */ + public function test_interface(Traversable $iterator) { + echo get_class($iterator); + } + + /** + * 첫번째 파라미터는 callable 이어야 함. + */ + public function test_callable(callable $callback, $data) { + call_user_func($callback, $data); + } +} + +// Another example class +class OtherClass { + public $var = 'Hello World'; +} +?> +]]> + </programlisting> + <para> + 타입 힌트를 충족하지 않을시 fatal error 결과. + </para> + <programlisting role="php"> +<![CDATA[ +<?php +// 각 클래스의 인스턴스 +$myclass = new MyClass; +$otherclass = new OtherClass; + +// Fatal Error: Argument 1 must be an object of class OtherClass +$myclass->test('hello'); + +// Fatal Error: Argument 1 must be an instance of OtherClass +$foo = new stdClass; +$myclass->test($foo); + +// Fatal Error: Argument 1 must not be null +$myclass->test(null); + +// Works: Prints Hello World +$myclass->test($otherclass); + +// Fatal Error: Argument 1 must be an array +$myclass->test_array('a string'); + +// Works: Prints the array +$myclass->test_array(array('a', 'b', 'c')); + +// Works: Prints ArrayObject +$myclass->test_interface(new ArrayObject(array())); + +// Works: Prints int(1) +$myclass->test_callable('var_dump', 1); +?> +]]> + </programlisting> + <para> + 타입힌트는 함수에서도 동작합니다: + </para> + <programlisting role="php"> +<![CDATA[ +<?php +// An example class +class MyClass { + public $var = 'Hello World'; +} + +/** + * A test function + * + * First parameter must be an object of type MyClass + */ +function myFunction(MyClass $foo) { + echo $foo->var; +} + +// Works +$myclass = new MyClass; +myFunction($myclass); +?> +]]> + </programlisting> + <para> + 타입힌트에 NULL을 지정할 수 있습니다: + </para> + <programlisting role="php"> +<![CDATA[ +<?php + +/* NULL 값 허용 */ +function test(stdClass $obj = NULL) { + +} + +test(NULL); +test(new stdClass); + +?> +]]> + </programlisting> + </example> + </sect1> + +<!-- Keep this comment at the end of the file +Local variables: +mode: sgml +sgml-omittag:t +sgml-shorttag:t +sgml-minimize-attributes:nil +sgml-always-quote-attributes:t +sgml-indent-step:1 +sgml-indent-data:t +indent-tabs-mode:nil +sgml-parent-document:nil +sgml-default-dtd-file:"~/.phpdoc/manual.ced" +sgml-exposed-tags:nil +sgml-local-catalogs:nil +sgml-local-ecat-files:nil +End: +vim600: syn=xml fen fdm=syntax fdl=2 si +vim: et tw=78 syn=sgml +vi: ts=1 sw=1 +--> Property changes on: phpdoc/kr/trunk/language/oop5/typehinting.xml ___________________________________________________________________ Added: svn:eol-style + native Added: svn:keywords + Id Rev Revision Date LastChangedDate LastChangedRevision Author LastChangedBy HeadURL URL

« previous php.doc.kr (#1977) next »