<?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>El Blog de Luis &#187; PHP</title>
	<atom:link href="http://lgallardo.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://lgallardo.com</link>
	<description>http://lgallardo.com &#62; Un poco de Linux, PS3, PSP y tecnología</description>
	<lastBuildDate>Mon, 06 Feb 2012 13:52:21 +0000</lastBuildDate>
	<language>es</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Clases y Objetos en PHP</title>
		<link>http://lgallardo.com/2009/06/26/clases-y-objetos-en-phpphps-classes-and-objects/</link>
		<comments>http://lgallardo.com/2009/06/26/clases-y-objetos-en-phpphps-classes-and-objects/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 03:17:46 +0000</pubDate>
		<dc:creator>Luis Gallardo</dc:creator>
				<category><![CDATA[Programación]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://lgallardo.com/?p=1134</guid>
		<description><![CDATA[Antes de comenzar a trabajar con clases y objetos, debemos conocer primero la diferencia entre la programación tradicional y la programación orientada a objetos (POO). En la programación tradicional los problemas son resueltos ejecutando una acción tras otra, en otras palabras, escribes un programa donde las acciones son ejecutadas una por una para resolver un [...]]]></description>
			<content:encoded><![CDATA[<p><a title="blocks" href="http://static.flickr.com/2585/3664450394_df2b4a6bb6.jpg"><img class="alignleft" src="http://static.flickr.com/2585/3664450394_df2b4a6bb6_t.jpg" alt="blocks" width="67" height="80" /></a>Antes de comenzar a trabajar con clases y objetos, debemos conocer primero la diferencia entre la programación tradicional y la programación orientada a objetos (POO). En la programación tradicional los problemas son resueltos ejecutando una acción tras otra, en otras palabras, escribes un programa donde las acciones son ejecutadas una por una para resolver un problema.</p>
<p>Por ejemplo, si deseas sumar dos números en PHP, basta con el siguiente código:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?
<span style="color: #666666; font-style: italic;">// Define dos numeros</span>
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Suma ambos numeros</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;a+b=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$c</span><span style="color: #339933;">;</span>
?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<p><span id="more-1134"></span></p>
<h2>Programación Orientada a Objetos (POO)</h2>
<p>En POO los problemas se resuelven desde otro punto de vista. Tienes que ver el problema (y su solución) como objetos con propiedades y comportamiento. Por ejemplo, puedes definir un objeto &#8220;escritorio&#8221; con algunas propiedades (ancho, alto, profundidad, color, etc) y comportamiento (estático). Si ahora piensas en un carro, algunas de las propiedades pueden ser el número de ruedas, modelo, año, etc, y su comportamiento puede ser &#8220;moviéndose&#8221; o &#8220;detenido&#8221;.</p>
<p>Ahora vamos a pensar en un ejemplo práctico. Imagina que quieres sumar dos números complejos. En PHP no hay una representación para los números complejos, es decir, no puedes ejecutar esta suma así no más:</p>
<blockquote>
<pre>2+3j
<span style="text-decoration: underline;">1+2j</span> +
3+5J</pre>
</blockquote>
<p>Debes resolver este problema con objetos. Primero que nada, debes escribir una clase (la definición del objeto)  para los números complejos donde definirás sus propiedades y comportamiento. En este caso las propiedades son la parte real e imaginaria, y su comportamiento son las operaciones que puedes ejecutar con dos números (sumar, restar, multiplicar, dividir). Vamos a ver el código PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?
<span style="color: #000000; font-weight: bold;">class</span> Complejo<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Propiedades</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">// La parte real</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">// La parte imaginaria </span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Constructor (Inicialización del objeto)</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>x <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>y <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Comportamiento</span>
    <span style="color: #000000; font-weight: bold;">function</span> sumar<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Creamos un nuevo objeto: el número complejo (0,0)</span>
        <span style="color: #000088;">$c</span>     <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Complejo<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Sumamos la parte real</span>
        <span style="color: #000088;">$c</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>x     <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>x <span style="color: #339933;">+</span> <span style="color: #000088;">$b</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>x<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Sumamos la parte imaginaria</span>
        <span style="color: #000088;">$c</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>y    <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>y <span style="color: #339933;">+</span> <span style="color: #000088;">$b</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>y<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Devolvemos el número complejo resultante</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">function</span> restar<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Aquí la parte real e imaginaria se restan y son</span>
        <span style="color: #666666; font-style: italic;">// pasadas como argumentos para un nuevo objeto,</span>
        <span style="color: #666666; font-style: italic;">// el cual será retornado como resultado</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Complejo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>x<span style="color: #339933;">-</span><span style="color: #000088;">$b</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>x<span style="color: #339933;">,</span><span style="color: #000088;">$a</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>y<span style="color: #339933;">-</span><span style="color: #000088;">$b</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<p>Ahora podemos guardar este código como Complejo.php. Aquí omití las otras dos operaciones (multiplicar y dividir).</p>
<h2>Probando nuestros Números Complejos de PHP</h2>
<p>Para probar si la clase funciona, podemos usarlo en un script como este:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?
<span style="color: #b1b100;">require</span> <span style="color: #0000ff;">&quot;Complejo.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Definimos un nuevo número complejo</span>
<span style="color: #000088;">$numero</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Complejo<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Parte real: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$numero</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>x<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&amp;lt;br&amp;gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Parte imaginaria: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$numero</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>y<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&amp;lt;br&amp;gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Ahora vamos a probar la suma</span>
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Complejo<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Complejo<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Suma $a y $b</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>sumar<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Mostrar el resultado</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;La suma es: (&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$c</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>x<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$c</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>y<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<h2>Referencias:</h2>
<ul>
<li><a href="http://us3.php.net/manual/en/language.oop5.php" target="_blank">Classes and Objects (PHP5)</a></li>
<li><a href="http://en.wikipedia.org/wiki/Complex_number" target="_blank">Complex Numbers</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://lgallardo.com/2009/06/26/clases-y-objetos-en-phpphps-classes-and-objects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

