mirror of
https://github.com/macintoshplus/doc-fr.git
synced 2026-03-25 17:32:07 +01:00
325 lines
7.6 KiB
XML
325 lines
7.6 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: f5e5b54129045a7d02c5285a88cea0abff8ffb6f Maintainer: yannick Status: ready -->
|
|
<!-- Reviewed: no -->
|
|
<sect1 xml:id="language.oop5.visibility" xmlns="http://docbook.org/ns/docbook">
|
|
<title>Visibilité</title>
|
|
<para>
|
|
La visibilité d'une propriété, d'une méthode ou (à partir de PHP 7.1.0) une constante peut
|
|
être définie en préfixant sa déclaration avec un mot-clé : <literal>public</literal>,
|
|
<literal>protected</literal>, ou
|
|
<literal>private</literal>.
|
|
Les éléments déclarés comme publics sont accessibles partout.
|
|
L'accès aux éléments protégés est limité à la classe elle-même, ainsi
|
|
qu'aux classes qui en héritent et parente.
|
|
L'accès aux éléments privés est uniquement réservé à la classe qui les a définis.
|
|
</para>
|
|
|
|
<sect2 xml:id="language.oop5.visiblity-members">
|
|
<title>Visibilité des propriétés</title>
|
|
<para>
|
|
Les propriétés des classes peuvent être définies comme publiques, privées
|
|
ou protégées. Les propriétés déclarées sans explicitement utiliser un
|
|
mot-clef de visibilité seront automatiquement définies comme publiques.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Déclaration de propriétés</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
/**
|
|
* Définition de MyClass
|
|
*/
|
|
class MyClass
|
|
{
|
|
public $public = 'Public';
|
|
protected $protected = 'Protected';
|
|
private $private = 'Private';
|
|
|
|
function printHello()
|
|
{
|
|
echo $this->public;
|
|
echo $this->protected;
|
|
echo $this->private;
|
|
}
|
|
}
|
|
|
|
$obj = new MyClass();
|
|
echo $obj->public; // Fonctionne
|
|
echo $obj->protected; // Erreur fatale
|
|
echo $obj->private; // Erreur fatale
|
|
$obj->printHello(); // Affiche Public, Protected et Private
|
|
|
|
|
|
/**
|
|
* Définition de MyClass2
|
|
*/
|
|
class MyClass2 extends MyClass
|
|
{
|
|
// On peut redéclarer les propriétés publics ou protégés, mais pas ceux privés
|
|
public $public = 'Public2';
|
|
protected $protected = 'Protected2';
|
|
|
|
function printHello()
|
|
{
|
|
echo $this->public;
|
|
echo $this->protected;
|
|
echo $this->private;
|
|
}
|
|
}
|
|
|
|
$obj2 = new MyClass2();
|
|
echo $obj2->public; // Fonctionne
|
|
echo $obj2->protected; // Erreur fatale
|
|
echo $obj2->private; // Indéfini
|
|
$obj2->printHello(); // Affiche Public2, Protected2 et Undefined (Indéfini)
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</sect2>
|
|
|
|
<sect2 xml:id="language.oop5.visiblity-methods">
|
|
<title>Visibilité des méthodes</title>
|
|
<para>
|
|
Les méthodes des classes peuvent être définies comme publiques, privées
|
|
ou protégées. Les méthodes déclarées sans explicitement utiliser un
|
|
mot-clef de visibilité seront automatiquement définies comme publiques.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Déclaration de méthodes</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
/**
|
|
* Définition de MyClass
|
|
*/
|
|
class MyClass
|
|
{
|
|
// Déclare un constructeur public
|
|
public function __construct() { }
|
|
|
|
// Déclare une méthode publique
|
|
public function MyPublic() { }
|
|
|
|
// Déclare une méthode protégée
|
|
protected function MyProtected() { }
|
|
|
|
// Déclare une méthode privée
|
|
private function MyPrivate() { }
|
|
|
|
// Celle-ci sera publique
|
|
function Foo()
|
|
{
|
|
$this->MyPublic();
|
|
$this->MyProtected();
|
|
$this->MyPrivate();
|
|
}
|
|
}
|
|
|
|
$myclass = new MyClass;
|
|
$myclass->MyPublic(); // Fonctionne
|
|
$myclass->MyProtected(); // Erreur fatale
|
|
$myclass->MyPrivate(); // Erreur fatale
|
|
$myclass->Foo(); // Public, Protected et Private fonctionnent
|
|
|
|
|
|
/**
|
|
* Définition de MyClass2
|
|
*/
|
|
class MyClass2 extends MyClass
|
|
{
|
|
// Celle-ci sera publique
|
|
function Foo2()
|
|
{
|
|
$this->MyPublic();
|
|
$this->MyProtected();
|
|
$this->MyPrivate(); // Erreur fatale
|
|
}
|
|
}
|
|
|
|
$myclass2 = new MyClass2;
|
|
$myclass2->MyPublic(); // Fonctionne
|
|
$myclass2->Foo2(); // Public et Protected fonctionnent, non Private
|
|
|
|
class Bar
|
|
{
|
|
public function test() {
|
|
$this->testPrivate();
|
|
$this->testPublic();
|
|
}
|
|
|
|
public function testPublic() {
|
|
echo "Bar::testPublic\n";
|
|
}
|
|
|
|
private function testPrivate() {
|
|
echo "Bar::testPrivate\n";
|
|
}
|
|
}
|
|
|
|
class Foo extends Bar
|
|
{
|
|
public function testPublic() {
|
|
echo "Foo::testPublic\n";
|
|
}
|
|
|
|
private function testPrivate() {
|
|
echo "Foo::testPrivate\n";
|
|
}
|
|
}
|
|
|
|
$myFoo = new Foo();
|
|
$myFoo->test(); // Bar::testPrivate
|
|
// Foo::testPublic
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</sect2>
|
|
|
|
<sect2 xml:id="language.oop5.visiblity-constants">
|
|
<title>Constant Visibility</title>
|
|
<para>
|
|
À partir de PHP 7.1.0, les constantes de classes peuvent être définies
|
|
comme public, privée ou protégée. Les constantes déclarées sans mot clé
|
|
de visibilité explicite sont définies en tant que public.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Déclaration de constantes à partir de PHP 7.1.0</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
/**
|
|
* Déclarons MyClass
|
|
*/
|
|
class MyClass
|
|
{
|
|
// Déclarons une constante public
|
|
public const MY_PUBLIC = 'public';
|
|
|
|
// Déclarons une constante protégée
|
|
protected const MY_PROTECTED = 'protected';
|
|
|
|
// Déclarons une constante privée
|
|
private const MY_PRIVATE = 'private';
|
|
|
|
public function foo()
|
|
{
|
|
echo self::MY_PUBLIC;
|
|
echo self::MY_PROTECTED;
|
|
echo self::MY_PRIVATE;
|
|
}
|
|
}
|
|
|
|
$myclass = new MyClass();
|
|
MyClass::MY_PUBLIC; // Fonctionne
|
|
MyClass::MY_PROTECTED; // Erreur fatale
|
|
MyClass::MY_PRIVATE; // Erreur fatale
|
|
$myclass->foo(); // Public, Protégée et Privée fonctionne
|
|
|
|
|
|
/**
|
|
* Define MyClass2
|
|
*/
|
|
class MyClass2 extends MyClass
|
|
{
|
|
// This is public
|
|
function foo2()
|
|
{
|
|
echo self::MY_PUBLIC;
|
|
echo self::MY_PROTECTED;
|
|
echo self::MY_PRIVATE; // Erreur fatale
|
|
}
|
|
}
|
|
|
|
$myclass2 = new MyClass2;
|
|
echo MyClass2::MY_PUBLIC; // Fonctionne
|
|
$myclass2->foo2(); // Public et Protégée fonctionne, mais pas Privée
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</sect2>
|
|
|
|
<sect2 xml:id="language.oop5.visibility-other-objects">
|
|
<title>Visibilité depuis d'autres objets</title>
|
|
<para>
|
|
Les objets de mêmes types ont accès aux membres privés et protégés les uns des autres,
|
|
même s'ils ne sont pas la même instance. Ceci est dû au fait que
|
|
les détails spécifiques de l'implémentation sont déjà connus en interne
|
|
par ces objets.
|
|
</para>
|
|
<example>
|
|
<title>Accès aux membres privés d'un objet du même type</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
class Test
|
|
{
|
|
private $foo;
|
|
|
|
public function __construct($foo)
|
|
{
|
|
$this->foo = $foo;
|
|
}
|
|
|
|
private function bar()
|
|
{
|
|
echo 'Accès à la méthode privée.';
|
|
}
|
|
|
|
public function baz(Test $other)
|
|
{
|
|
// Nous pouvons modifier la propriété privée :
|
|
$other->foo = 'Bonjour';
|
|
var_dump($other->foo);
|
|
|
|
// Nous pouvons également appeler la méthode privée :
|
|
$other->bar();
|
|
}
|
|
}
|
|
|
|
$test = new Test('test');
|
|
|
|
$test->baz(new Test('other'));
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
string(7) "Bonjour"
|
|
Accès à la méthode privée.
|
|
]]>
|
|
</screen>
|
|
</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
|
|
-->
|