mirror of
https://github.com/php/doc-fr.git
synced 2026-03-24 07:02:06 +01:00
111 lines
2.6 KiB
XML
111 lines
2.6 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: e587d0655e426f97b3fcb431453da5030e743b23 Maintainer: yannick Status: ready -->
|
|
<!-- Reviewed: yes -->
|
|
|
|
<sect1 xml:id="language.types.object">
|
|
<title>Les objets</title>
|
|
|
|
<sect2 xml:id="language.types.object.init">
|
|
<title>Initialisation des objets</title>
|
|
|
|
<para>
|
|
Pour créer un nouvel objet, utiliser le mot clé <literal>new</literal>
|
|
afin d'instancier une classe :
|
|
</para>
|
|
|
|
<example>
|
|
<title>Construction d'objet</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
class foo
|
|
{
|
|
function do_foo()
|
|
{
|
|
echo "Doing foo.";
|
|
}
|
|
}
|
|
|
|
$bar = new foo;
|
|
$bar->do_foo();
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
|
|
<simpara>
|
|
Pour une discussion complète, voir le chapitre sur
|
|
<link linkend="language.oop5">les classes et les objets</link>.
|
|
</simpara>
|
|
|
|
</sect2>
|
|
|
|
<sect2 xml:id="language.types.object.casting">
|
|
<title>Conversion en un objet</title>
|
|
<para>
|
|
Si un objet est converti en un objet, il ne sera pas modifié.
|
|
Si une valeur de n'importe quel type est convertie en un objet,
|
|
une nouvelle instance de la classe interne <classname>stdClass</classname>
|
|
sera créée. Si la valeur est &null;, la nouvelle instance sera vide.
|
|
Un <type>array</type> se convertit en <type>object</type> avec les propriétés
|
|
nommées au regard des clés avec leurs valeurs correspondantes. Il est à noter que
|
|
dans ce cas, avant PHP 7.2.0 les clés numériques ont été inaccessibles à
|
|
moins d'être itérées.
|
|
</para>
|
|
|
|
<example>
|
|
<title>Conversion en un objet</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$obj = (object) array('1' => 'foo');
|
|
var_dump(isset($obj->{'1'})); // affiche 'bool(true)'
|
|
|
|
// Déprécié depuis PHP 8.1
|
|
var_dump(key($obj)); // affiche 'string(1) "1"'
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
|
|
<para>
|
|
Pour n'importe quel autre type, un membre appelé <literal>scalar</literal>
|
|
contiendra la valeur.
|
|
</para>
|
|
|
|
<example>
|
|
<title>Conversion <literal>(object)</literal></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$obj = (object) 'ciao';
|
|
echo $obj->scalar; // Affiche : 'ciao'
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
|
|
</sect2>
|
|
</sect1>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|