File: functions.xml

package info (click to toggle)
phpdoc 20020310-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 35,272 kB
  • ctags: 354
  • sloc: xml: 799,767; php: 1,395; cpp: 500; makefile: 200; sh: 140; awk: 51
file content (325 lines) | stat: -rw-r--r-- 10,189 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?xml version="1.0" encoding="utf-8"?>
 <chapter id="functions">
  <title>함수 (Functions)</title>

  <sect1 id="functions.user-defined">
   <title>사용자 정의 함수 (User-defined functions)</title>
 
   <para>
	함수는 다음과 같이 정의한다. :
 
    <informalexample>
     <programlisting role="php">
function foo ($arg_1, $arg_2, ..., $arg_n) {
    echo "Example function.\n";
    return $retval;
}
     </programlisting>
    </informalexample>
   </para>
     
   <simpara>
	함수 안에는 다른 함수나 <link linkend="keyword.class">class</link>의 선언 등을 포함한 모든 가능한 PHP 코드가 사용될 수 있다.
   </simpara>
   <simpara>
	PHP3에서는 함수는 그 함수가 사용되기 이전에 선언되어 있어야 하였으나 
	PHP4에서는 이런 제약이 없어졌다.
   </simpara>
   <simpara>
	PHP는 function overloading을 지원하지 않고, 
	이미 정의된 함수를 재정의하거나 없애지 못한다.
   </simpara>
   <simpara>
	PHP3에서는 함수 파라메터의 기본값을 설정해주는 것
	(<link linkend="functions.arguments.default">Default argument values</link>를 보라.)은 가능해도, 
	파라메터의 개수를 가변으로 설정하는 것은 불가능했지만, PHP4는 두가지 모두 가능하다. 
	자세한 내용은 <link linkend="functions.variable-arg-list">Variable-length argument lists</link>와
	<function>func_num_args</function>, <function>func_get_arg</function>, 
	<function>func_get_args</function> 함수의 설명을 보기 바란다.
   </simpara>

  </sect1>
 
  <sect1 id="functions.arguments">
   <title>Function arguments</title>
 
   <simpara>
	argument list를 통해 함수에 어떤 정보를 넘겨줄 수 있다. 
	이 argument list는 쉼표(,)로 나뉘어진 변수나 상수의 list이다. 
   </simpara> 
   <para>
	PHP는 passing by value(기본적으로 이것을 사용)와 
	<link linkend="functions.arguments.by-reference">passing by reference</link>, 
	<link linkend="functions.arguments.default">default argument values</link>의 3가지 방법을 제공한다. 
	가변 길이(Variable-length) argument list는 PHP4이후에서만 제공된다. 
	자세한 내용은 <link linkend="functions.variable-arg-list">Variable-length argument lists</link>와 
     <function>func_num_args</function>, <function>func_get_arg</function>, 
     <function>func_get_args</function> 함수의 설명을 보기 바란다. 
	그러나 PHP3에서도 배열을 통한 전달을 사용한다면 비슷한 효과를 낼 수 있다.
    <informalexample>
     <programlisting role="php">
function takes_array($input) {
    echo "$input[0] + $input[1] = ", $input[0]+$input[1];
}
     </programlisting>
    </informalexample>
   </para>
 
   <sect2 id="functions.arguments.by-reference">
    <title>Making arguments be passed by reference</title>
 
    <simpara>
	기본값으로 함수의 인수(argument)들은 값으로 전달된다(passed by value). 
	함수내에서 변화시킨 값을 함수 밖에서도 적용시키려면 pass by reference로 인수를 넘겨야 한다.
    </simpara>
    <para>
	어떤 함수의 인수를 항상 pass by reference로 넘기려 한다면, 
	함수를 선언할 때 ampersand(&amp;)를 인수의 앞에 붙여주면 된다. :
     <informalexample>
      <programlisting role="php">
function add_some_extra(&amp;$string) {
    $string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str;    // outputs 'This is a string, and something extra.'
      </programlisting>
     </informalexample>
    </para>
 
    <para>
	만약 기본값은 by value 로 하지만 필요에 따라 by reference로 호출하고 싶다면 
	함수 호출 시에 인수의 앞에 &amp;를 붙이면 된다. :
 
     <informalexample>
      <programlisting role="php">
function foo ($bar) {
    $bar .= ' and something extra.';
}
$str = 'This is a string, ';
foo ($str);
echo $str;    // outputs 'This is a string, '
foo (&amp;$str);
echo $str;    // outputs 'This is a string, and something extra.'
      </programlisting>
     </informalexample>
    </para>

   </sect2>
 
   <sect2 id="functions.arguments.default">
    <title>Default argument values</title>
 
    <para>
	스칼라 인수(argument)에 대해서는 다음과 같이 C++ 과 비슷하게 기본값을 정해줄 수 있다. : 
     <informalexample>
      <programlisting role="php">
function makecoffee ($type = "cappucino") {
    return "Making a cup of $type.\n";
}
echo makecoffee ();
echo makecoffee ("espresso");
      </programlisting>
     </informalexample>
    </para>
 
    <para>
     위의 코드의 실행 결과는 다음과 같다 :
 
     <screen>
Making a cup of cappucino.
Making a cup of espresso.
     </screen>
    </para>
 
    <simpara>
	default 값은 반드시 상수이어야 한다. 
	(예를들어) 변수나 class의 멤버를 사용해서는 안된다.
    </simpara>
    <para>
	default argument를 사용할 때, default가 되는 인수들은 non-default인 인수들보다 
	오른쪽에 위치해야 한다. 그렇지 않으면 원하는 결과가 나오지 않는다. 다음을 보자. :
     <informalexample>
      <programlisting role="php">
function makeyogurt ($type = "acidophilus", $flavour) {
    return "Making a bowl of $type $flavour.\n";
}
 
echo makeyogurt ("raspberry");   // won't work as expected
      </programlisting>
     </informalexample>
    </para>
 
    <para>
     위 코드의 실행 결과는 다음과 같다 :
 
     <screen>
Warning: Missing argument 2 in call to makeyogurt() in 
/usr/local/etc/httpd/htdocs/php3test/functest.html on line 41
Making a bowl of raspberry .
     </screen>
    </para>
 
    <para>
     그러면 이제 위의 것과 아래것을 비교해 보자. : 
 
     <informalexample>
      <programlisting role="php">
function makeyogurt ($flavour, $type = "acidophilus") {
    return "Making a bowl of $type $flavour.\n";
}
 
echo makeyogurt ("raspberry");   // works as expected
      </programlisting>
     </informalexample>
    </para>
 
    <para>
     이 예제의 실행 결과는 다음과 같다. : 
 
     <screen>
Making a bowl of acidophilus raspberry.
     </screen>
    </para>

   </sect2>

   <sect2 id="functions.variable-arg-list">
    <title>Variable-length argument lists</title>
	
    <simpara>
	PHP4에서는 사용자 정의 함수에 가변 길이(Variable-length) argument list를 제공한다. 
	<function>func_num_args</function>, <function>func_get_arg</function>, 
	<function>func_get_args</function> 함수를 사용하여 쉽게 사용할 수 있다.
    </simpara>

    <simpara>
	특별한 문법이 사용되지도 않고, 함수의 정의시나 사용시 argument list는 
	보통의 경우와 동일하게 사용하면 된다.
    </simpara>

   </sect2>

  </sect1>
 
  <sect1 id="functions.returning-values">
   <title>Returning values</title>
 
   <para>
	함수는 return 문을 통해 함수값을 돌려줄 수 있다. 
	list나 object를 포함한 어떤 type도 돌려질 수 있다.
    <informalexample>
     <programlisting role="php">
function square ($num) {
    return $num * $num;
}
echo square (4);   // outputs '16'.
     </programlisting>
    </informalexample>
   </para>
      
   <para>
	여러값을 돌려줄 수는  없다. 
	그러나 list를 돌려줌으로써 비슷한 일을 할 수 있다. :
 
    <informalexample>
     <programlisting role="php">
function small_numbers() {
    return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();
     </programlisting>
    </informalexample>
   </para>
   <para>
    To return a reference from a function, you have to use
    the reference operator &amp; in both the function declaration and
    when assigning the returned value to a variable:
    <informalexample>
     <programlisting role="php">
function &amp;returns_reference() {
    return $someref;
}

$newref =&amp;returns_reference();
     </programlisting>
    </informalexample>
   </para>

  </sect1>
 
  <sect1 id="functions.old-syntax">
   <title><literal>old_function</literal></title>
 
   <simpara>
	<literal>old_function</literal> 구문은 PHP/FI2에서와 동일한 함수 사용법을 제공한다. 
	(function대신 old_function을 사용한다는 점은 제외하고)
   </simpara>
   <simpara>
	이것을 사용하는 것은 매우 좋지 않은 방법이다. 
	이것이 사용될 때는 PHP/FI2->PHP3 변환기에서 뿐이다. 
   </simpara>
   <warning>
    <para>
	<literal>old_function</literal>으로 정의된 함수들은 PHP의 내부 코드에서 호출될 수 없다. 
	이 말은 <function>usort</function>나, <function>array_walk</function>, 
	<function>register_shutdown_function</function>같은 함수에 사용할 수 없다는 의미이다. 
	이를 해결하기 위해서는 이 <literal>old_function</literal>으로 선언된 함수를 호출하는 
	PHP3 형태의 함수를 만들어 사용하는 것이다.
    </para>
   </warning>

  </sect1>

  <sect1 id="functions.variable-functions">
   <title>Variable functions</title>

   <para>
	PHP는 가변 함수(variable functions) 개념을 지원한다. 이것은 변수명 뒤에 괄호가 왔을 때, 
	PHP는 그 이름을 가진 함수를 찾아 실행한다는 것을 의미한다. 
	이 기능은  callbacks, function table 등의 기능에 사용하면 매우 유용하게 사용할 수 있다
   </para>

   <para>
    <example>
     <title>Variable function example</title>
     <programlisting role="php">
&lt;?php
function foo() {
    echo "In foo()&lt;br>\n";
}

function bar( $arg = '' ) {
    echo "In bar(); argument was '$arg'.&lt;br>\n";
}

$func = 'foo';
$func();
$func = 'bar';
$func( 'test' );
?>
     </programlisting>
    </example>
   </para>

  </sect1>

 </chapter>
 
 <!-- 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:"../../manual.ced"
 sgml-exposed-tags:nil
 sgml-local-catalogs:nil
 sgml-local-ecat-files:nil
 End:
 -->