<?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; classes</title>
	<atom:link href="http://technocil.com/tag/classes/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>Conheça a função __autoload e esqueça que um dia precisou usar listas gigantes de includes</title>
		<link>http://technocil.com/2009/04/30/conheca-a-funcao-__autoload-e-esqueca-que-um-dia-precisou-listas-gigantes-de-includes/</link>
		<comments>http://technocil.com/2009/04/30/conheca-a-funcao-__autoload-e-esqueca-que-um-dia-precisou-listas-gigantes-de-includes/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 15:49:01 +0000</pubDate>
		<dc:creator>Tobias</dc:creator>
				<category><![CDATA[Desenvolvimento web]]></category>
		<category><![CDATA[Geral]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[autoload]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[__autoload]]></category>

		<guid isPermaLink="false">http://technocil.com/?p=313</guid>
		<description><![CDATA[E ae gente, blz?
Bom o post de hoje é pequeno e rápido e com certeza incrívelmente útil, vou mostrar hoje uma técnica que estou usando pra facilitar minha vida na hora de desenvolver meus códigos, o assunto de hoje é o __autoload, uma funçãozinha que está me economizando preciosos minutos no desenvolvimento.
O funcionamento dele é [...]]]></description>
			<content:encoded><![CDATA[<p>E ae gente, blz?</p>
<p>Bom o post de hoje é pequeno e rápido e com certeza incrívelmente útil, vou mostrar hoje uma técnica que estou usando pra facilitar minha vida na hora de desenvolver meus códigos, o assunto de hoje é o <a href="http://br.php.net/__autoload"><em>__autoload</em></a>, uma funçãozinha que está me economizando preciosos minutos no desenvolvimento.</p>
<p>O funcionamento dele é básico, e funciona assim:</p>
<p>Quando você tenta instanciar uma classe o php verifica se essa classe já existe, se não existir ele tenta carregar ela de acordo com a função <em>__autoload</em> que estiver no script.</p>
<p>ex:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class_name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">require_once</span> <span style="color: #000088;">$class_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>digamos que em algum momento do meu código eu tentei instanciar a classe Groups</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$groups</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Groups<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>se a classe Groups não existir até o momento o php vai tentar incluir o arquivo Groups.php (de acordo com a minha função <em>__autoload</em>) antes de disparar um erro. Ta mas ele sempre vai procurar só pelo nome da classe? depende&#8230; tudo depende de como você faz a sua função <em>__autoload</em>, veja o exemplo abaixo para entender.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class_name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'my/path/to/classes/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$class_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>nesse exemplo acima o php vai procurar pela classe na path determinada na função antes de disparar o erro.</p>
<p>Bom é isso, só pra terminar vou dar uma dica que eu estou usando aqui, que é simplesmente adicionar as paths de onde as classes estão ao <em><a href="http://br.php.net/set_include_path">include_path</a></em>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</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: #990000;">set_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">get_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'./libs/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class_name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">require_once</span> <span style="color: #000088;">$class_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Vou explicar o código:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">set_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">get_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'./libs/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Adiciona ao <em>include_path</em> do php a pasta ./libs/</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class_name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">require_once</span> <span style="color: #000088;">$class_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Define a função __autoload que irá procurar pelas classes quando forem instanciadas.</p>
<p>Ufa&#8230; cabou, é isso ai gente, como sempre dúvidas, sugestões e puxões de orelha nos comentários por favor :D.</p>
<p>abraço e até a próxima,<br />
ps. esse foi o post mais rápido que eu já postei, da idéia de postar até o fim da revisão do texto demorou 26 min oO.</p>
]]></content:encoded>
			<wfw:commentRss>http://technocil.com/2009/04/30/conheca-a-funcao-__autoload-e-esqueca-que-um-dia-precisou-listas-gigantes-de-includes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
