<!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>Using Phar Archives: the Phar and PharData class</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="phar.using.stream.html">Using Phar Archives: the phar stream wrapper</a></div> <div class="next" style="text-align: right; float: right;"><a href="phar.creating.html">Creating Phar Archives</a></div> <div class="up"><a href="phar.using.html">Using Phar Archives</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="phar.using.object" class="section"> <h2 class="title">Using Phar Archives: the Phar and PharData class</h2> <p class="para"> The <a href="class.phar.html" class="classname">Phar</a> class supports reading and manipulation of Phar archives, as well as iteration through inherited functionality of the <a href="class.recursivedirectoryiterator.html" class="classname">RecursiveDirectoryIterator</a> class. With support for the <a href="class.arrayaccess.html" class="classname">ArrayAccess</a> interface, files inside a Phar archive can be accessed as if they were part of an associative array. </p> <p class="para"> The <a href="class.phardata.html" class="classname">PharData</a> class extends the <a href="class.phar.html" class="classname">Phar</a>, and allows creating and modifying non-executable (data) tar and zip archives even if <em>phar.readonly</em>=1 in php.ini. As such, <span class="function"><a href="phardata.setalias.html" class="function">PharData::setAlias()</a></span> and <span class="function"><a href="phardata.setstub.html" class="function">PharData::setStub()</a></span> are both disabled as the concept of alias and stub are unique to executable phar archives. </p> <p class="para"> It is important to note that when creating a Phar archive, the full path should be passed to the <a href="class.phar.html" class="classname">Phar</a> object constructor. Relative paths will fail to initialize. </p> <p class="para"> Assuming that <em>$p</em> is a Phar object initialized as follows: </p> <p class="para"> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$p </span><span style="color: #007700">= new </span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/myphar.phar'</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #DD0000">'myphar.phar'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> An empty Phar archive will be created at <em>/path/to/myphar.phar</em>, or if <em>/path/to/myphar.phar</em> already exists, it will be opened again. The literal <em>myphar.phar</em> demonstrates the concept of an alias that can be used to reference <em>/path/to/myphar.phar</em> in URLs as in: </p> <p class="para"> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// these two calls to file_get_contents() are equivalent if<br />// /path/to/myphar.phar has an explicit alias of "myphar.phar"<br />// in its manifest, or if the phar was initialized with the<br />// previous example's Phar object setup<br /></span><span style="color: #0000BB">$f </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'phar:///path/to/myphar.phar/whatever.txt'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$f </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'phar://myphar.phar/whatever.txt'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> With the newly created <em>$p</em> <a href="class.phar.html" class="classname">Phar</a> object, the following is possible: <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <em>$a = $p['file.php']</em> creates a <a href="class.pharfileinfo.html" class="classname">PharFileInfo</a> class that refers to the contents of <em>phar://myphar.phar/file.php</em> </span> </li> <li class="listitem"> <span class="simpara"> <em>$p['file.php'] = $v</em> creates a new file (<em>phar://myphar.phar/file.php</em>), or overwrites an existing file within <em>myphar.phar</em>. <em>$v</em> can be either a string or an open file pointer, in which case the entire contents of the file will be used to create the new file. Note that <em>$p->addFromString('file.php', $v)</em> is functionally equivalent to the above. Also possible is to add the contents of a file with <em>$p->addFile('/path/to/file.php', 'file.php')</em>. Lastly, an empty directory can be created with <em>$p->addEmptyDir('empty')</em>. </span> </li> <li class="listitem"> <span class="simpara"> <em>isset($p['file.php'])</em> can be used to determine whether <em>phar://myphar.phar/file.php</em> exists within <em>myphar.phar</em>. </span> </li> <li class="listitem"> <span class="simpara"> <em>unset($p['file.php'])</em> erases <em>phar://myphar.phar/file.php</em> from <em>myphar.phar</em>. </span> </li> </ul> </p> <p class="para"> In addition, the <a href="class.phar.html" class="classname">Phar</a> object is the only way to access Phar-specific metadata, through <span class="function"><a href="phar.getmetadata.html" class="function">Phar::getMetadata()</a></span>, and the only way to set or retrieve a Phar archive's PHP loader stub through <span class="function"><a href="phar.getstub.html" class="function">Phar::getStub()</a></span> and <span class="function"><a href="phar.setstub.html" class="function">Phar::setStub()</a></span>. Additionally, compression for the entire Phar archive at once can only be manipulated using the <a href="class.phar.html" class="classname">Phar</a> class. </p> <p class="para"> The full list of <a href="class.phar.html" class="classname">Phar</a> object functionality is documented below. </p> <p class="para"> The <a href="class.pharfileinfo.html" class="classname">PharFileInfo</a> class extends the <a href="class.splfileinfo.html" class="classname">SplFileInfo</a> class, and adds several methods for manipulating Phar-specific details of a file contained within a Phar, such as manipulating compression and metadata. </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="phar.using.stream.html">Using Phar Archives: the phar stream wrapper</a></div> <div class="next" style="text-align: right; float: right;"><a href="phar.creating.html">Creating Phar Archives</a></div> <div class="up"><a href="phar.using.html">Using Phar Archives</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>