11.09.2018 Views

Aprender a desarrollar un sitio Web con PHP y MySQL

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Por ejemplo, para aplicar el método añadir_<strong>un</strong>_kilo() en la clase Animal:<br />

<br />

public f<strong>un</strong>ction comer()<br />

{<br />

//Método para acceder a las propiedades<br />

//color y peso<br />

}<br />

public f<strong>un</strong>ction moverse()<br />

{<br />

//Método para acceder a las propiedades<br />

//color y peso<br />

}<br />

public f<strong>un</strong>ction añadir_<strong>un</strong>_kilo()<br />

{<br />

$this->peso = $this->peso + 1;<br />

}<br />

Cuando llama al método añadir_<strong>un</strong>_kilo(), añadirá 1 al peso actual y por lo tanto el peso final<br />

será 11.<br />

Las propiedades se declaran <strong>con</strong> el símbolo $, pero se llaman <strong>con</strong> $this sin este símbolo.<br />

5. Utilización de la clase<br />

Como primer paso, tenemos que crear <strong>un</strong> archivo que <strong>con</strong>tenga <strong>un</strong> código <strong>PHP</strong> Animal.class.php.<br />

Para utilizar la clase Animal, debe incluirla en la página donde la quiere llamar.<br />

Cree <strong>un</strong>a página uso.php y escriba el siguiente código:<br />

<br />

Ahora que ha cargado la clase, puede instanciarla, es decir, crear <strong>un</strong> objeto que tenga como modelo la<br />

clase Animal:<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!