<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc'; $TOC = array(); $TOC_DEPRECATED = array(); $PARENTS = array(); include_once dirname(__FILE__) ."/toc/phar.using.inc"; $setup = array ( 'home' => array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'en', ), 'this' => array ( 0 => 'phar.using.object.php', 1 => 'Using Phar Archives: the Phar and PharData class', ), 'up' => array ( 0 => 'phar.using.php', 1 => 'Using Phar Archives', ), 'prev' => array ( 0 => 'phar.using.stream.php', 1 => 'Using Phar Archives: the phar stream wrapper', ), 'next' => array ( 0 => 'phar.creating.php', 1 => 'Creating Phar Archives', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/phar/using.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?> <div id="phar.using.object" class="section"> <h2 class="title">Using Phar Archives: the Phar and PharData class</h2> <p class="para"> The <span class="classname"><a href="class.phar.php" class="classname">Phar</a></span> class supports reading and manipulation of Phar archives, as well as iteration through inherited functionality of the <span class="classname"><a href="class.recursivedirectoryiterator.php" class="classname">RecursiveDirectoryIterator</a></span> class. With support for the <span class="classname"><a href="class.arrayaccess.php" class="classname">ArrayAccess</a></span> interface, files inside a Phar archive can be accessed as if they were part of an associative array. </p> <p class="para"> The <span class="classname"><a href="class.phardata.php" class="classname">PharData</a></span> class extends the <span class="classname"><a href="class.phar.php" class="classname">Phar</a></span>, and allows creating and modifying non-executable (data) tar and zip archives even if <code class="literal">phar.readonly</code>=1 in php.ini. As such, <span class="function"><a href="phardata.setalias.php" class="function">PharData::setAlias()</a></span> and <span class="function"><a href="phardata.setstub.php" 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 <span class="classname"><a href="class.phar.php" class="classname">Phar</a></span> object constructor. Relative paths will fail to initialize. </p> <p class="para"> Assuming that <code class="literal">$p</code> 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 <code class="literal">/path/to/myphar.phar</code>, or if <code class="literal">/path/to/myphar.phar</code> already exists, it will be opened again. The literal <code class="literal">myphar.phar</code> demonstrates the concept of an alias that can be used to reference <code class="literal">/path/to/myphar.phar</code> 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 <code class="literal">$p</code> <span class="classname"><a href="class.phar.php" class="classname">Phar</a></span> object, the following is possible: <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <code class="literal">$a = $p['file.php']</code> creates a <span class="classname"><a href="class.pharfileinfo.php" class="classname">PharFileInfo</a></span> class that refers to the contents of <code class="literal">phar://myphar.phar/file.php</code> </span> </li> <li class="listitem"> <span class="simpara"> <code class="literal">$p['file.php'] = $v</code> creates a new file (<code class="literal">phar://myphar.phar/file.php</code>), or overwrites an existing file within <code class="literal">myphar.phar</code>. <code class="literal">$v</code> 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 <code class="literal">$p->addFromString('file.php', $v)</code> is functionally equivalent to the above. Also possible is to add the contents of a file with <code class="literal">$p->addFile('/path/to/file.php', 'file.php')</code>. Lastly, an empty directory can be created with <code class="literal">$p->addEmptyDir('empty')</code>. </span> </li> <li class="listitem"> <span class="simpara"> <code class="literal">isset($p['file.php'])</code> can be used to determine whether <code class="literal">phar://myphar.phar/file.php</code> exists within <code class="literal">myphar.phar</code>. </span> </li> <li class="listitem"> <span class="simpara"> <code class="literal">unset($p['file.php'])</code> erases <code class="literal">phar://myphar.phar/file.php</code> from <code class="literal">myphar.phar</code>. </span> </li> </ul> </p> <p class="para"> In addition, the <span class="classname"><a href="class.phar.php" class="classname">Phar</a></span> object is the only way to access Phar-specific metadata, through <span class="function"><a href="phar.getmetadata.php" 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.php" class="function">Phar::getStub()</a></span> and <span class="function"><a href="phar.setstub.php" class="function">Phar::setStub()</a></span>. Additionally, compression for the entire Phar archive at once can only be manipulated using the <span class="classname"><a href="class.phar.php" class="classname">Phar</a></span> class. </p> <p class="para"> The full list of <span class="classname"><a href="class.phar.php" class="classname">Phar</a></span> object functionality is documented below. </p> <p class="para"> The <span class="classname"><a href="class.pharfileinfo.php" class="classname">PharFileInfo</a></span> class extends the <span class="classname"><a href="class.splfileinfo.php" class="classname">SplFileInfo</a></span> 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><?php manual_footer($setup); ?>