<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Technócil &#187; protected</title>
	<atom:link href="http://technocil.com/tag/protected/feed/" rel="self" type="application/rss+xml" />
	<link>http://technocil.com</link>
	<description></description>
	<lastBuildDate>Tue, 24 Aug 2010 21:24:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>OOP no PHP: Visibilidade, definições de public, protected, private e static</title>
		<link>http://technocil.com/2008/12/11/oop-no-php-visibilidade-definicoes-de-public-protected-private-e-static/</link>
		<comments>http://technocil.com/2008/12/11/oop-no-php-visibilidade-definicoes-de-public-protected-private-e-static/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 03:52:00 +0000</pubDate>
		<dc:creator>Tobias</dc:creator>
				<category><![CDATA[Desenvolvimento web]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[escopo]]></category>
		<category><![CDATA[private]]></category>
		<category><![CDATA[protected]]></category>
		<category><![CDATA[public]]></category>
		<category><![CDATA[static]]></category>

		<guid isPermaLink="false">http://technocil.com/?p=137</guid>
		<description><![CDATA[E ae blz?
Bom inaugurando a minha participação aqui no blog resolvi começar com uma série de artigos sobre OOP e PHP, no primeiro post eu vou explicar o que significam as palavrinhas public, protected, private e static, o que significam e a função de cada uma delas em nossas classes.
Só pra constar essa série inicial [...]]]></description>
			<content:encoded><![CDATA[<p>E ae blz?</p>
<p>Bom inaugurando a minha participação aqui no blog resolvi começar com uma série de artigos sobre <acronym title="Programação Orientação a objetos">OOP</acronym> e PHP, no primeiro post eu vou explicar o que significam as palavrinhas public, protected, private e static, o que significam e a função de cada uma delas em nossas classes.</p>
<p>Só pra constar essa série inicial terá 5 posts com os seguintes assuntos.</p>
<ul>
<li>Visibilidade, definições de public, protected, private e static</li>
<li>Heranças e Interfaces, extendendo e implementando classes</li>
<li>Reaproveitamento, Sobrescrevendo métodos</li>
<li>Overloading, utilizando os métodos mágicos do PHP</li>
<li>Improviso, simulando a sobrecarga de métodos no PHP</li>
</ul>
<p>Bom já que estamos com tudo resolvido vamos começar a brincadeira.</p>
<p>As definições de escopo são bem simples e não há necessidade de se extender falando delas.</p>
<p><em><strong>public</strong></em> é a liberal das definições de escopo, todo método ou atributo de uma classe definido como<br />
public pode ser acessado de qualquer lugar.</p>
<p><em><strong>protected</strong></em> é um pouco mais contida só pode ser acessada de dentro da classe e de suas classes filhas.</p>
<p><em><strong>private</strong></em> como o nome já diz é totalmente restrita e só pode ser acessada de dentro da classe.</p>
<p>Exemplo abaixo:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Exemplo <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$publicInfo</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Sou uma informação pública'</span><span style="color: #339933;">;</span>
  protected <span style="color: #000088;">$protectedInfo</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Sou uma informação protegida'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$privateInfo</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Sou uma informação restrita'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPublicInfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">publicInfo</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  protected <span style="color: #000000; font-weight: bold;">function</span> getProtectedInfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">protectedInfo</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getPrivateInfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">privateInfo</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$exemplo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Exemplo<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Imprime Sou uma informação pública</span>
<span style="color: #000088;">$exemplo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPublicInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Retorna Fatal error: Call to protected method Exemplo::getProtectedInfo() from context</span>
<span style="color: #000088;">$exemplo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProtectedInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Retorna Fatal error: Call to private method Exemplo::getPrivateInfo() from context</span>
<span style="color: #000088;">$exemplo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPrivateInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Tudo bem aparentemente, então vamos pra função de static.</p>
<p><em><strong>static</strong></em> permite que algum método ou atributo de uma classe seja chamado sem a necessidade de instanciar a classe. O método/atributo é chamado da seguinte forma:</p>
<p>NomeDaClasse::NomeDoMetodo() ou<br />
NomeDaClasse::$NomeDoAtributo</p>
<p>Exemplo:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> ExemploStatic <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000088;">$atributo</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Eu tenho alguma informação'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> fazAlgumaCoisa<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Eu faço alguma coisa'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Imprime Eu tenho alguma informação</span>
<span style="color: #b1b100;">echo</span> ExemploStatic<span style="color: #339933;">::</span><span style="color: #000088;">$atributo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Imprime Eu faço alguma coisa</span>
ExemploStatic<span style="color: #339933;">::</span><span style="color: #004000;">fazAlgumaCoisa</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>É por hora acho que já ta de bom tamanho, você pode encontrar mais informações sobre <a href="http://br.php.net/manual/pt_BR/language.oop5.visibility.php">public, private e protected</a> e também sobre a <a href="http://br.php.net/manual/pt_BR/language.oop5.static.php">static</a> na documentação oficial do <a href="http://www.php.net">php</a>.</p>
<p>Ainda ficou com alguma dúvida? sim? bom os comentários tão aqui pra isso, mas se você não ficou com dúvidas e quer dar um puxão de orelha no menino aqui, os comentários também server pra isso.</p>
<p>Um abraço e até o próximo post.</p>
]]></content:encoded>
			<wfw:commentRss>http://technocil.com/2008/12/11/oop-no-php-visibilidade-definicoes-de-public-protected-private-e-static/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
