phar.using.object.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 133 行

HTML
133
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Using Phar Archives: the Phar and PharData class</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="phar.using.html">Using Phar Archives</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="http://www.php.net/~helly/php/ext/spl/classRecursiveDirectoryIterator.html" class="link external">&raquo; <a href="class.recursivedirectoryiterator.html" class="classname">RecursiveDirectoryIterator</a></a>  class.  With support for the <b class="classname">ArrayAccess</b>  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  <i>phar.readonly</i>=1 in php.ini.  As such,  <a href="phardata.setalias.html" class="function">PharData::setAlias()</a> and <a href="phardata.setstub.html" class="function">PharData::setStub()</a>  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 <i>$p</i> 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">&lt;?php<br />$p&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'myphar.phar'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>   </div>  </div> </p> <p class="para">  An empty Phar archive will be created at <i>/path/to/myphar.phar</i>,  or if <i>/path/to/myphar.phar</i> already exists, it will be opened  again.  The literal <i>myphar.phar</i> demonstrates the concept of an alias  that can be used to reference <i>/path/to/myphar.phar</i> 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">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;these&nbsp;two&nbsp;calls&nbsp;to&nbsp;file_get_contents()&nbsp;are&nbsp;equivalent&nbsp;if<br />//&nbsp;/path/to/myphar.phar&nbsp;has&nbsp;an&nbsp;explicit&nbsp;alias&nbsp;of&nbsp;"myphar.phar"<br />//&nbsp;in&nbsp;its&nbsp;manifest,&nbsp;or&nbsp;if&nbsp;the&nbsp;phar&nbsp;was&nbsp;initialized&nbsp;with&nbsp;the<br />//&nbsp;previous&nbsp;example's&nbsp;Phar&nbsp;object&nbsp;setup<br /></span><span style="color: #0000BB">$f&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;</span><span style="color: #007700">=&nbsp;</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">?&gt;</span></span></code></div>   </div>  </div> </p> <p class="para">  With the newly created <i>$p</i> <a href="class.Phar.html" class="classname">Phar</a> object,  the following is possible:  <ul class="itemizedlist">   <li class="listitem">    <span class="simpara">     <i>$a = $p[&#039;file.php&#039;]</i> creates a <a href="class.PharFileInfo.html" class="classname">PharFileInfo</a>     class that refers to the contents of <i>phar://myphar.phar/file.php</i>    </span>   </li>   <li class="listitem">    <span class="simpara">     <i>$p[&#039;file.php&#039;] = $v</i> creates a new file     (<i>phar://myphar.phar/file.php</i>), or overwrites     an existing file within <i>myphar.phar</i>.  <i>$v</i>     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     <i>$p-&gt;addFromString(&#039;file.php&#039;, $v)</i> is functionally     equivalent to the above.  Also possible is to add the contents of a file     with <i>$p-&gt;addFile(&#039;/path/to/file.php&#039;, &#039;file.php&#039;)</i>.     Lastly, an empty directory can be created with     <i>$p-&gt;addEmptyDir(&#039;empty&#039;)</i>.    </span>   </li>   <li class="listitem">    <span class="simpara">     <i>isset($p[&#039;file.php&#039;])</i> can be used to determine     whether <i>phar://myphar.phar/file.php</i> exists within     <i>myphar.phar</i>.    </span>   </li>   <li class="listitem">    <span class="simpara">     <i>unset($p[&#039;file.php&#039;])</i> erases     <i>phar://myphar.phar/file.php</i> from     <i>myphar.phar</i>.    </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  <a href="phar.getmetadata.html" class="function">Phar::getMetaData()</a>,  and the only way to set or retrieve a Phar archive&#039;s PHP loader stub through  <a href="phar.getstub.html" class="function">Phar::getStub()</a> and  <a href="phar.setstub.html" class="function">Phar::setStub()</a>.  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="http://www.php.net/~helly/php/ext/spl/classSplFileInfo.html" class="link external">&raquo; <b class="classname">SplFileInfo</b></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 style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="phar.using.html">Using Phar Archives</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>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?