Foros del Web » Programando para Internet » PHP » Frameworks y PHP orientado a objetos »

Problema con clase

Estas en el tema de Problema con clase en el foro de Frameworks y PHP orientado a objetos en Foros del Web. Hola chicos, tengo un pequeño problema con una clase. el problema es que es estoy recibiendo el error: Cita: Warning: Cannot use a scalar value ...
  #1 (permalink)  
Antiguo 20/11/2008, 08:19
Avatar de juaniquillo
Colaborador
 
Fecha de Ingreso: noviembre-2005
Ubicación: San Juan, Puerto Rico
Mensajes: 5.745
Antigüedad: 18 años, 5 meses
Puntos: 281
Problema con clase

Hola chicos, tengo un pequeño problema con una clase. el problema es que es estoy recibiendo el error:

Cita:
Warning: Cannot use a scalar value as an array in /home/donq/public_html/store/include/shoppingbasket.class.php4.php on line 65
Debe ser un problema de PHP4 en mi servidor remoto ya que en mi servidor local que tiene PHP 5.2.6 todo funciona bien. Yo traté de adaptar la clase lo mejor que pude para usarla en PHP4 pero parece que no pude debido a mi limitado conocimiento en POO. Esta es la clase:

Código PHP:
Ver original
  1. <?php
  2. /*
  3. SHOPPING BASKET CLASS
  4.  
  5. USAGE:
  6. ------
  7.  
  8. $cart = new ShoppingBasket; Initialize class
  9. $cart->SaveCookie(TRUE); Set option to save items ina cookie or not. Cookie valid for 1 day.
  10. $cart->AddToBasket('ITEM_ID', QTY); Add an item to the basket
  11. $cart->RemoveFromBasket('ITEM_ID', QTY); Remove item form basket
  12. $cart->DeleteFromBasket('ITEM_ID'); Removes all of item selected
  13. $cart->EmptyBasket('ITEM_ID' QTY); Clear the basket
  14. $cart->GetBasketQty(); Get qty of items in the basket
  15. $cart->GetBasket(); Returns basket items as an array ITEM_ID => QTY
  16.  
  17. */
  18.  
  19. /**
  20.  * ShoppingBasket
  21.  *
  22.  * A simple shopping basket class used to add and delete items from a session based shopping cart
  23.  * @package ShoppingBasket
  24.  * @author Dave Nicholson <[email protected]>
  25.  * @link [url]http://davenicholson.co.uk[/url]
  26.  * @copyright 2008
  27.  * @version 0.1
  28.  * @access public
  29.  */
  30. class ShoppingBasket {
  31.  
  32.     var $cookieName = 'ckBasket';
  33.     var $cookieExpire = 86400; // One day
  34.     var $saveCookie = TRUE;
  35.  
  36.     /**
  37.      * ShoppingBasket::__construct()
  38.      *
  39.      * Construct function. Parses cookie if set.
  40.      * @return
  41.      */
  42.     function ShoppingBasket() {
  43.  
  44.         session_start();
  45.  
  46.         if (!isset($_SESSION['cart']) && (isset($_COOKIE[$this->cookieName]))) {
  47.             $_SESSION['cart'] = unserialize(base64_decode($_COOKIE[$this->cookieName]));
  48.         }
  49.  
  50.     }
  51.  
  52.     /**
  53.      * ShoppingBasket::AddToBasket()
  54.      *
  55.      * Adds item to basket. If $id already exists in array then qty updated
  56.      * @param mixed $id - ID of item
  57.      * @param integer $qty - Qty of items to be added to cart
  58.      * @return bool
  59.      */
  60.     function AddToBasket($id, $qty = 1) {
  61.  
  62.         if (isset($_SESSION['cart'][$id])) {
  63.             $_SESSION['cart'][$id] = $_SESSION['cart'][$id] + $qty;
  64.         } else {
  65.             $_SESSION['cart'][$id] = $qty;
  66.         }
  67.         $this->SetCookie();
  68.         return true;
  69.     }
  70.  
  71.     /**
  72.      * ShoppingBasket::RemoveFromBasket()
  73.      *
  74.      * Removes item from basket. If final qty less than 1 then item deleted.
  75.      * @param mixed $id - Id of item
  76.      * @param integer $qty - Qty of items to be removed to cart
  77.      * @see DeleteFromBasket()
  78.      * @return bool
  79.      */
  80.     function RemoveFromBasket($id, $qty = 1) {
  81.  
  82.         if (isset($_SESSION['cart'][$id])) {
  83.             $_SESSION['cart'][$id] = $_SESSION['cart'][$id] - $qty;
  84.         }
  85.  
  86.         if ($_SESSION['cart'][$id] <= 0) {
  87.             $this->DeleteFromBasket($id);
  88.         }
  89.        
  90.         $this->SetCookie();
  91.         return true;
  92.         exit();
  93.     }
  94.  
  95.     /**
  96.      * ShoppingBasket::DeleteFromBasket()
  97.      *
  98.      * Completely removes item from basket
  99.      * @param mixed $id
  100.      * @return bool
  101.      */
  102.     function DeleteFromBasket($id) {
  103.         unset($_SESSION['cart'][$id]);
  104.         $this->SetCookie();
  105.         return true;
  106.         exit();
  107.     }
  108.  
  109.     /**
  110.      * ShoppingBasket::GetBasket()
  111.      *
  112.      * Returns the basket session as an array of item => qty
  113.      * @return array $itemArray
  114.      */
  115.     function GetBasket() {
  116.         if (isset($_SESSION['cart'])) {
  117.             foreach ($_SESSION['cart'] as $k => $v) {
  118.                 $itemArray[$k] = $v;
  119.             }
  120.             return $itemArray;
  121.             exit();
  122.         } else {
  123.             return false;
  124.         }
  125.     }
  126.  
  127.     /**
  128.      * ShoppingBasket::UpdateBasket()
  129.      *
  130.      * Updates a basket item with a specific qty
  131.      * @param mixed $id - ID of item
  132.      * @param mixed $qty - Qty of items in basket
  133.      * @return bool
  134.      */
  135.     function UpdateBasket($id, $qty) {
  136.  
  137.         $qty = ($qty == '') ? 0 : $qty;
  138.  
  139.         if (isset($_SESSION['cart'][$id])) {
  140.             $_SESSION['cart'][$id] = $qty;
  141.  
  142.             if ($_SESSION['cart'][$id] <= 0) {
  143.                 $this->DeleteItem($id);
  144.                 return true;
  145.                 exit();
  146.             }
  147.             $this->SetCookie();
  148.             return true;
  149.             exit();
  150.  
  151.         } else {
  152.             return false;
  153.         }
  154.  
  155.     }
  156.  
  157.     /**
  158.      * ShoppingBasket::GetBasketQty()
  159.      *
  160.      * Returns the total amount of items in the basket
  161.      * @return int quantity of items in basket
  162.      */
  163.     function GetBasketQty() {
  164.         if (isset($_SESSION['cart'])) {
  165.             $qty = 0;
  166.             foreach ($_SESSION['cart'] as $item) {
  167.                 $qty = $qty + $item;
  168.             }
  169.             return $qty;
  170.         } else {
  171.             return 0;
  172.         }
  173.     }
  174.  
  175.     /**
  176.      * ShoppingBasket::EmptyBasket()
  177.      *
  178.      * Completely removes the basket session
  179.      * @return
  180.      */
  181.     function EmptyBasket() {
  182.         unset($_SESSION['cart']);
  183.         $this->SetCookie();
  184.         return true;
  185.     }
  186.  
  187.   /**
  188.    * ShoppingBasket::SetCookie()
  189.    *
  190.    * Creates cookie of basket items
  191.    * @return bool
  192.    */
  193.     function SetCookie() {
  194.  
  195.         if ($this->saveCookie) {
  196.             $string = base64_encode(serialize($_SESSION['cart']));
  197.             setcookie($this->cookieName, $string, time() + $this->cookieExpire, '/');
  198.             return true;
  199.         }
  200.        
  201.         return false;
  202.     }
  203.    
  204.   /**
  205.    * ShoppingBasket::SaveCookie()
  206.    *
  207.    * Sets save cookie option
  208.    * @param bool $bool
  209.    * @return bool
  210.    */
  211.     function SaveCookie($bool = TRUE) {
  212.         $this->saveCookie = $bool;
  213.         return true;
  214.     }
  215.  
  216. }
  217.  
  218. ?>

esta es la parte del error:

Código PHP:
Ver original
  1. function AddToBasket($id, $qty = 1) {
  2.  
  3.         if (isset($_SESSION['cart'][$id])) {
  4.             $_SESSION['cart'][$id] = $_SESSION['cart'][$id] + $qty;
  5.         } else {
  6.             $_SESSION['cart'][$id] = $qty;
  7.         }
  8.         $this->SetCookie();
  9.         return true;
  10.     }

Gracias y saludos.
__________________
Por fin.. tengo algo parecido a un blog
Y por lo visto ya estoy escribiendo...
  #2 (permalink)  
Antiguo 20/11/2008, 09:59
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: Problema con clase

Mmmm prueba que tengas register_globals en off, puede ser que si lo tienes en On se este reescribiendo la variable.

Saludos.
  #3 (permalink)  
Antiguo 20/11/2008, 10:21
Avatar de juaniquillo
Colaborador
 
Fecha de Ingreso: noviembre-2005
Ubicación: San Juan, Puerto Rico
Mensajes: 5.745
Antigüedad: 18 años, 5 meses
Puntos: 281
Respuesta: Problema con clase

Ja, muchas gracias GatorV. Eso era exactamente lo que pasaba. Esto ya me estaba volviendo loco. ¿Cuándo tendré un servidor remoto decente en alguno de mis trabajos?

Saludos. Te debo una cerveza.
__________________
Por fin.. tengo algo parecido a un blog
Y por lo visto ya estoy escribiendo...
  #4 (permalink)  
Antiguo 20/11/2008, 10:37
Avatar de juaniquillo
Colaborador
 
Fecha de Ingreso: noviembre-2005
Ubicación: San Juan, Puerto Rico
Mensajes: 5.745
Antigüedad: 18 años, 5 meses
Puntos: 281
Respuesta: Problema con clase

bueno, además de hacer lo de las register_global tuve que hacer lo siguiente:

Tuve que explícitamente convertir la variable en array si no lo es, así que terminé cambiando la función:

Código PHP:
Ver original
  1. function AddToBasket($id, $qty = 1) {
  2.  
  3.         if (isset($_SESSION['cart'][$id])) {
  4.             $_SESSION['cart'][$id] = $_SESSION['cart'][$id] + $qty;
  5.         } else {
  6.             $_SESSION['cart'][$id] = $qty;
  7.         }
  8.         $this->SetCookie();
  9.         return true;
  10.     }

Por:

Código PHP:
Ver original
  1. function AddToBasket($id, $qty = 1) {
  2.  
  3.         if (isset($_SESSION['cart'][$id])) {
  4.             if (!is_array($_SESSION['cart'][$id])) $_SESSION['cart'][$id] = array();
  5.             $_SESSION['cart'][$id] = $_SESSION['cart'][$id] + $qty;
  6.         } else {
  7.             if (!is_array($_SESSION['cart'][$id])) $_SESSION['cart'][$id] = array();
  8.             $_SESSION['cart'][$id] = $qty;
  9.         }
  10.         $this->SetCookie();
  11.         return true;
  12.     }

Espero que a alguien le sirva. Saludos.
__________________
Por fin.. tengo algo parecido a un blog
Y por lo visto ya estoy escribiendo...
  #5 (permalink)  
Antiguo 20/11/2008, 10:59
Avatar de juaniquillo
Colaborador
 
Fecha de Ingreso: noviembre-2005
Ubicación: San Juan, Puerto Rico
Mensajes: 5.745
Antigüedad: 18 años, 5 meses
Puntos: 281
Respuesta: Problema con clase

y también, la opción del cookie provocaba un error en el carro de compras. Tuve que desactivar la opción por ahora. Ya trabajaré con ella mas tarde.
__________________
Por fin.. tengo algo parecido a un blog
Y por lo visto ya estoy escribiendo...
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:43.