<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.types.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'language.types.object.php',
    1 => 'Objects',
    2 => 'Objects',
  ),
  'up' => 
  array (
    0 => 'language.types.php',
    1 => 'Типи',
  ),
  'prev' => 
  array (
    0 => 'language.types.array.php',
    1 => 'Масив (array)',
  ),
  'next' => 
  array (
    0 => 'language.types.enumerations.php',
    1 => 'Enumerations',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/types/object.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.types.object" class="sect1">
 <h2 class="title">Objects</h2>

 <div class="sect2" id="language.types.object.init">
  <h3 class="title">Object Initialization</h3>

  <p class="para">
   To create a new <span class="type"><a href="language.types.object.php" class="type object">object</a></span>, use the <code class="literal">new</code> statement
   to instantiate a class:
  </p>

  <div class="example" id="example-1">
   <p><strong>Приклад #1 Object Construction</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">foo<br /></span><span style="color: #007700">{<br />    function </span><span style="color: #0000BB">do_foo</span><span style="color: #007700">()<br />    {<br />        echo </span><span style="color: #DD0000">"Doing foo."</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= new </span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$bar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">do_foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <p class="simpara">
   For a full discussion, see the
   <a href="language.oop5.php" class="link">Classes and Objects</a> chapter.
  </p>

 </div>

 <div class="sect2" id="language.types.object.casting">
  <h3 class="title">Converting to object</h3>

  <p class="para">
   If an <span class="type"><a href="language.types.object.php" class="type object">object</a></span> is converted to an <span class="type"><a href="language.types.object.php" class="type object">object</a></span>, it is not
   modified. If a value of any other type is converted to an
   <span class="type"><a href="language.types.object.php" class="type object">object</a></span>, a new instance of the <span class="classname"><a href="class.stdclass.php" class="classname">stdClass</a></span>
   built-in class is created. If the value was <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>, the new instance will be
   empty. An <span class="type"><a href="language.types.array.php" class="type array">array</a></span> converts to an <span class="type"><a href="language.types.object.php" class="type object">object</a></span> with properties
   named by keys and corresponding values. Note that in this case before PHP 7.2.0 numeric keys
   have been inaccessible unless iterated.
  </p>

  <div class="example" id="example-2">
   <p><strong>Приклад #2 Casting to an Object</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$obj </span><span style="color: #007700">= (object) array(</span><span style="color: #DD0000">'1' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;{</span><span style="color: #DD0000">'1'</span><span style="color: #007700">})); </span><span style="color: #FF8000">// outputs 'bool(true)'<br /><br />// Deprecated as of PHP 8.1<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">key</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">)); </span><span style="color: #FF8000">// outputs 'string(1) "1"'<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <p class="para">
   For any other value, a member variable named <code class="literal">scalar</code> will contain
   the value.
  </p>

  <div class="example" id="example-3">
   <p><strong>Приклад #3 <code class="literal">(object)</code> cast</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$obj </span><span style="color: #007700">= (object) </span><span style="color: #DD0000">'ciao'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">scalar</span><span style="color: #007700">;  </span><span style="color: #FF8000">// outputs 'ciao'<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

 </div>
</div><?php manual_footer($setup); ?>