<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Construct a new file object.</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="class.splfileobject.html">SplFileObject</a></div> <div class="next" style="text-align: right; float: right;"><a href="splfileobject.current.html">SplFileObject::current</a></div> <div class="up"><a href="class.splfileobject.html">SplFileObject</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="splfileobject.construct" class="refentry"> <div class="refnamediv"> <h1 class="refname">SplFileObject::__construct</h1> <p class="verinfo">(PHP 5 >= 5.1.0)</p><p class="refpurpose"><span class="refname">SplFileObject::__construct</span> — <span class="dc-title">Construct a new file object.</span></p> </div> <div class="refsect1 description" id="refsect1-splfileobject.construct-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="methodname"><strong>SplFileObject::__construct</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$open_mode</code><span class="initializer"> = "r"</span></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$use_include_path</code><span class="initializer"> = false</span></span> [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span> ]]] )</div> <p class="para rdfs-comment"> Construct a new file object. </p> </div> <div class="refsect1 parameters" id="refsect1-splfileobject.construct-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">filename</code></em></span> <dd> <p class="para"> The file to read. </p> <div class="tip"><strong class="tip">Tip</strong><p class="simpara">A URL can be used as a filename with this function if the <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">fopen wrappers</a> have been enabled. See <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> for more details on how to specify the filename. See the <a href="wrappers.html" class="xref">Supported Protocols and Wrappers</a> for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.</p></div> </dd> </dt> <dt> <span class="term"><em><code class="parameter">open_mode</code></em></span> <dd> <p class="para"> The mode in which to open the file. See <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> for a list of allowed modes. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">use_include_path</code></em></span> <dd> <p class="para"> Whether to search in the <a href="ini.core.html#ini.include-path" class="link">include_path</a> for <em><code class="parameter">filename</code></em>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">context</code></em></span> <dd> <p class="para"> A valid context resource created with <span class="function"><a href="function.stream-context-create.html" class="function">stream_context_create()</a></span>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-splfileobject.construct-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> No value is returned. </p> </div> <div class="refsect1 errors" id="refsect1-splfileobject.construct-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> Throws a <a href="class.runtimeexception.html" class="classname">RuntimeException</a> if the <em><code class="parameter">filename</code></em> cannot be opened. </p> </div> <div class="refsect1 examples" id="refsect1-splfileobject.construct-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3946"> <p><strong>Example #1 <span class="methodname"><strong>SplFileObject::__construct()</strong></span> example</strong></p> <div class="example-contents"><p> This example opens the current file and iterates over its contents line by line. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$file </span><span style="color: #007700">= new </span><span style="color: #0000BB">SplFileObject</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">);<br />foreach (</span><span style="color: #0000BB">$file </span><span style="color: #007700">as </span><span style="color: #0000BB">$line_num </span><span style="color: #007700">=> </span><span style="color: #0000BB">$line</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Line </span><span style="color: #0000BB">$line_num</span><span style="color: #DD0000"> is </span><span style="color: #0000BB">$line</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Line 0 is <?php Line 1 is $file = new SplFileObject(__FILE__); Line 2 is foreach ($file as $line_num => $line) { Line 3 is echo "Line $line_num is $line"; Line 4 is } Line 5 is ?> </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-splfileobject.construct-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="methodname"><a href="splfileinfo.openfile.html" class="methodname" rel="rdfs-seeAlso">SplFileInfo::openFile()</a> - Gets an SplFileObject object for the file</span></li> <li class="member"> <span class="function"><a href="function.fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - Opens file or URL</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="class.splfileobject.html">SplFileObject</a></div> <div class="next" style="text-align: right; float: right;"><a href="splfileobject.current.html">SplFileObject::current</a></div> <div class="up"><a href="class.splfileobject.html">SplFileObject</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>