mirror of
https://github.com/php/doc-pt_br.git
synced 2026-03-24 07:02:09 +01:00
* Updates to abandoned/outdated pages in reference/array/functions/ * Updates to outdated/abandoned pages and new translations in reference/classobj/ * Updates to abandoned/outdated pages in reference/ctype/ * Correction to double-byte UTF-8 character which is similar to 'E' * Correction of translation * Updates to abandoned/outdated files and new translations in reference/dir/ * Updates to abandoned/outdated files in reference/mysqli/ * New translation of reference/sockets/functions/socket-atmark.xml * New translation of reference/stream/* * Corrections after QA scripts * Delete reference/stream/versions.xml Arquivo marcado para não tradução. --------- Co-authored-by: LEONARDO LARA RODRIGUES <leonardolara@github.com> Co-authored-by: alfsb <alfsb@users.noreply.github.com>
192 lines
4.2 KiB
XML
192 lines
4.2 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: 6f11457f11d91834e1240c3351d8c4e289371b6d Maintainer: felipe Status: ready --><!-- CREDITS: felipe,leonardolara -->
|
|
|
|
<appendix xml:id="classobj.examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
Neste exemplo, nós primeiramente definiremos uma classe base e uma
|
|
extensão da classe. A classe base descreve um vegetal comum,
|
|
que é comestível ou não e qual é sua cor. A sub-classe
|
|
<varname>Espinafre</varname> adiciona um método para cozinhá-lo e outro
|
|
para descobrir se ele está cozido.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Definições da Classe</title>
|
|
<para><varname>Vegetal</varname></para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
class Vegetal {
|
|
public $comestivel;
|
|
|
|
public $cor;
|
|
|
|
public function __construct($comestivel, $cor = "verde")
|
|
{
|
|
$this->comestivel = $comestivel;
|
|
$this->cor = $cor;
|
|
}
|
|
|
|
public function comestivel()
|
|
{
|
|
return $this->comestivel;
|
|
}
|
|
|
|
public function qual_cor()
|
|
{
|
|
return $this->cor;
|
|
}
|
|
}
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para><varname>Espinafre</varname></para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
class Espinafre extends Vegetal {
|
|
public $cozido = false;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct(true, "verde");
|
|
}
|
|
|
|
public function cozinhe()
|
|
{
|
|
$this->cozido = true;
|
|
}
|
|
|
|
public function esta_cozido()
|
|
{
|
|
return $this->cozido;
|
|
}
|
|
}
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
Agora serão instanciados objetos destas classes e exibidas informações
|
|
sobre elas, incluindo o parentesco entre elas.
|
|
Também serão definidas algumas funções úteis, principalmente para ter
|
|
uma saída bem formatada de variáveis.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>test_script.php</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
// register autoloader to load classes
|
|
spl_autoload_register();
|
|
|
|
function printProperties($obj)
|
|
{
|
|
foreach (get_object_vars($obj) as $prop => $val) {
|
|
echo "\t$prop = $val\n";
|
|
}
|
|
}
|
|
|
|
function printMethods($obj)
|
|
{
|
|
$arr = get_class_methods(get_class($obj));
|
|
foreach ($arr as $metodo) {
|
|
echo "\tfunção $metodo()\n";
|
|
}
|
|
}
|
|
|
|
function objetoPertenceA($obj, $classe)
|
|
{
|
|
if (is_subclass_of($obj, $classe)) {
|
|
echo "Objeto pertence à classe " . get_class($obj);
|
|
echo ", uma sub-classe de $classe\n";
|
|
} else {
|
|
echo "Objeto não pertence a uma sub-classe de $classe\n";
|
|
}
|
|
}
|
|
|
|
// instancia 2 objetos
|
|
|
|
$vegetal = new Vegetal(true, "azul");
|
|
$folha = new Espinafre();
|
|
|
|
// mostra informação sobre os objetos
|
|
echo "vegetal: CLASSE " . get_class($vegetal) . "\n";
|
|
echo "folha: CLASSE " . get_class($folha);
|
|
echo ", CLASSE PAI " . get_parent_class($folha) . "\n";
|
|
|
|
// mostra propriedades do vegetal
|
|
echo "\nvegetal: Propriedades\n";
|
|
printProperties($vegetal);
|
|
|
|
// e métodos de folha
|
|
echo "\nfolha: Métodos\n";
|
|
printMethods($folha);
|
|
|
|
echo "\nParentesco:\n";
|
|
objetoPertenceA($folha, Espinafre::class);
|
|
objetoPertenceA($folha, Vegetal::class);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&examples.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
vegetal: CLASSE Vegetal
|
|
folha: CLASSE Espinafre, CLASSE PAI Vegetal
|
|
|
|
vegetal: Propriedades
|
|
comestivel = 1
|
|
cor = azul
|
|
|
|
folha: Métodos
|
|
function __construct()
|
|
function cozinhe()
|
|
function esta_cozido()
|
|
function comestivel()
|
|
function qual_cor()
|
|
|
|
Parentesco:
|
|
Objeto não pertence a uma sub-classe de Espinafre
|
|
Objeto pertence à classe Espinafre, uma sub-classe de Vegetal
|
|
]]>
|
|
</screen>
|
|
<para>
|
|
Uma nota importante sobre o exemplo acima é que
|
|
o objeto <varname>$folha</varname> é uma instância da classe
|
|
<classname>Espinafre</classname> que é uma sub-classe de
|
|
<classname>Vegetal</classname>.
|
|
</para>
|
|
</example>
|
|
</para>
|
|
</appendix>
|
|
|
|
<!-- 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
|
|
-->
|