VoyForums
[ Show ]
Support VoyForums
[ Shrink ]
VoyForums Announcement: Programming and providing support for this service has been a labor of love since 1997. We are one of the few services online who values our users' privacy, and have never sold your information. We have even fought hard to defend your privacy in legal cases; however, we've done it with almost no financial support -- paying out of pocket to continue providing the service. Due to the issues imposed on us by advertisers, we also stopped hosting most ads on the forums many years ago. We hope you appreciate our efforts.

Show your support by donating any amount. (Note: We are still technically a for-profit company, so your contribution is not tax-deductible.) PayPal Acct: Feedback:

Donate to VoyForums (PayPal):

Login ] [ Contact Forum Admin ] [ Main index ] [ Post a new message ] [ Search | Check update time ]
Subject: Definicion de Class en PHP


Author:
Charlie Mayorga Giraldo
[ Next Thread | Previous Thread | Next Message | Previous Message ]
Date Posted: 14:01:32 04/16/02 Tue

CLASS

Una clase es una coleccion de variables y funciones que trabajan con estas variables. Una clase esta definida usando la siguiente sintaxis:

class Cart
{
var $items;

function add_item ($artnr, $num)
{
$this->items[$artnr] += $num;
}

function remove_item ($artnr, $num)
{
if ($this->items[$artnr] > $num) {
$this->items[$artnr] -= $num;
return true;
} else {
return false;
}
}
}
?>

Este ejemplo define una clase llamada Cart que consiste de un arreglo asociativo de articulos y 2 funciones para adicionar y remover items.

Las funciones en las clases no deben utilizar al principio de sus nombres los caracteres ( __ ), estos caracteres son reservados para funciones magicas del PHP.

Las clases son tipos de variables, es decir, Todo lo que aplica a una tipo de variable aplica a las clases. La unica diferencia radica en la construccion de la clase, que se realiza con el operador new.

$cart = new Cart;
$cart->add_item("10", 1);

$otro_cart = new Cart;
$otro_cart->add_item("0815", 3);
?>

Este ejemplo crea 2 instancias de la clase Cart. Ambas instancias cuentan con las funciones add_item y remove_item y con la variable $item, recuerde estas 2 instancias ocupan espacios de memoria diferentes.

Fin --- CHM

[ Next Thread | Previous Thread | Next Message | Previous Message ]


Post a message:
This forum requires an account to post.
[ Create Account ]
[ Login ]
[ Contact Forum Admin ]


Forum timezone: GMT-8
VF Version: 3.00b, ConfDB:
Before posting please read our privacy policy.
VoyForums(tm) is a Free Service from Voyager Info-Systems.
Copyright © 1998-2019 Voyager Info-Systems. All Rights Reserved.